VS 2008 での文字列変換

 VisualStudio 2008 では marshal_as がようやく入ります。
 それによって文字列変換がどうなるかというと、

std::string strHello = "Hello, Orcas";
String^ mstr = marshal_as(strHello);

std::wstring wstrHello = L"へろぅ、おるかす";
String^ mstr = marshal_as(wstrHello);

LPCTSTR ptrTstrHello = _T("Hello, Orcas");
String^ mstr = marshal_as(ptrTstrHello);

_bstr_t bstrHello("Hello, Orcas");
String^ mstr = marshal_as(bstrHello);

となります。
 当然のことながら、逆変換は

String^ mstr = "Hello, あんどれ";

std::string strHello = marshal_as(mstr);

std::wstring wstrHello = marshal_as(mstr);

_bstr_t bstrHello = marshal_as<_bstr_t>(mstr);

です。メモリ・アロケートが必要なものに関しては、marshal_context を経由します。

marshal_context context;
LPCTSTR ptstrHello = context.marshal_as(mstr);
BSTR bstrHello = context.marshal_as(mstr);

 marshal_as は文字列にしか用意してないそうなので、それ以外に関しては自前で実装だそうです。

 これでようやく、初期に約束されていた marshal_as の仕様が実現したと言うことですね。
 次はいよいよ混合型の実現を期待します。