template と delegate & native型 と CLI型 - コンパイルできなくて死亡
あぁもう、C++/CLI は変態さんだなぁもおおおおおおおおおおお!!!!(右手がグーのまま震えながら
今日はこんなコードではまりました @ Visual C++ 2008 Express Edition SP1(コードに深い意味はないです)
// C++/CLI struct NativeStruct {}; private ref class DelegateTest { public: delegate NativeStruct FNFunc(System::Object^); template<typename T> static NativeStruct funcT(System::Object^ obj) { // ... } static NativeStruct func1(System::Object^ obj) { // ... } static DelegateTest() { dic = gcnew MyDictionary(); dic[0] = gcnew FNFunc(&func1); // error C3352: 'funcT' : 指定された関数は delegate の型 'NativeStruct (System::Object ^)' と一致しません dic[1] = gcnew FNFunc(&funcT<int>); // error C3352: 'funcT' : 指定された関数は delegate の型 'NativeStruct (System::Object ^)' と一致しません dic[2] = gcnew FNFunc(&funcT<double>); } typedef System::Collections::Generic::Dictionary<int, FNFunc^> MyDictionary; static MyDictionary^ dic; };
…はて?
delegate のコンストラクタ(?)では、関数テンプレートを実体化したモノを投げており、その関数の型は合っているはずなんですが……。
引数の数・型も一緒だし、戻り値も一緒だし、static 関数だから this も不要だし……うーむ? template がなんか悪いのかしら? とか適当に予測つけてガリゴリ弄っても特に変化無し。
しかし! 実験しているうちにコードが通ったのです! その時のコードがこんなの!
// C++/CLI private ref class DelegateTest { public: delegate int FNFunc(System::Object^); template<typename T> static int funcT(System::Object^ obj) { // ... } static int func1(System::Object^ obj) { // ... } static DelegateTest() { dic = gcnew MyDictionary(); dic[0] = gcnew FNFunc(&func1); dic[1] = gcnew FNFunc(&funcT<int>); // OK dic[2] = gcnew FNFunc(&funcT<double>); // OK } typedef System::Collections::Generic::Dictionary<int, FNFunc^> MyDictionary; static MyDictionary^ dic; };
template関数の場合のみ native 型が通らないってどういう事だってばよ!! orz
…えぇー? これはもしかして仕様…なのかしら…それとも…? うーむ…
しかし、構造体なんか返すなよとか突っ込まれそうですけど(ぉ
実は、C++ native のライブラリを利用していて、8byteの共同体を返そうと思ったのですよ。 8byte ぐらい戻り値に指定しても良いじゃん? みたいな。
まぁでも、戻り値に指定出来ない事が判ってしまったので、仕方ないので参照取るかなぁ… < コレならOKっぽい
ところで、native型 と CLI型 って正しい呼び方なのかしら…(ぉ ^^;