
You don't need a to_string() function. You can construct a String
directly from a const char*:
std::string s;
String^ managed_str = gcnew String(s.c_str());
You certainly don't have to convert a literal, you can assign it directly:
String^ ms2 = "Hello";
A const char* is not a literal constant, and requires conversion, even
in VC++ 2003 and /clr:oldSntax. const char* is using 1 byte per
character, while String^ is using 2 bytes. Also, const char* uses
unmanaged memory, while String^ is garbage collected. They're not
compatible. In the old MC++, this conversion was implicit, but it was a
conversion function nonetheless.
You can not really have the same code compile for .NET and ISO C++. The
syntax is vastly different, just think about class vs ref class / value
class, or enum vs enum class, etc. You can compile ISO C++ syntax to
managed code, but ISO C++ declarations won't be accessible from outside
of the assembly, and ISO C++ types will be unmanaged types.
The following, however, will always work with both ISO C++ and C++/CLI,
without 0 overhead:
In the old MC++, you use to have to write S"Hello", which was much
worse. You no longer have to do that, all you need is the double quotes.
So the situation in C++/CLI is not worse, but much better than it used
to be.
In the old MC++, every time you did this, you had a conversion overhead,
whether you realized that or not:
const char* my_str = "Hello";
String* managed_str = my_str;
That's exactly the same overhead as the following in C++/CLI:
const char* my_str = "Hello";
String^ managed_str = gcnew String(my_str);
Tom