関数内関数その2
前回触れた関数内関数、引数をとる叙述関数の場合はbind使って
渡す事になるんだけど、ローカルの関数オブジェクトだとVC2005で
コンパイルエラーに。ローカルメンバ関数だと通るのでとりあえず
問題はないんだけど、なんでだろう??
#include <stdio.h> #include <vector> #include <algorithm> #include <boost/bind.hpp> //#define USE_GLOBALFUNCTION //#define USE_LOCALFUNCTOR #define USE_LOCALFUNCTION enum TestType { TYPE_A, TYPE_B, TYPE_C }; struct Test { TestType Type; Test(TestType InType):Type(InType){} }; #if defined(USE_GLOBALFUNCTION) // 普通の関数版 bool IsSameType(Test& InTest, TestType InType) { if (InType == InTest.Type) { return true; } return false; } #endif int main() { using namespace std; using namespace boost; vector<Test> Tests; Tests.push_back(Test(TYPE_A)); Tests.push_back(Test(TYPE_B)); Tests.push_back(Test(TYPE_C)); printf("Tests.size() = %d\n",Tests.size()); #if defined(USE_LOCALFUNCTOR) // 関数オブジェクト版 struct { bool operator()(Test& InTest, TestType InType) { if (InType == InTest.Type) { return true; } return false; } } IsSameType; #endif #if defined(USE_LOCALFUNCTION) // staticメンバー関数版 struct local{ static bool IsSameType(Test& InTest, TestType InType) { if (InType == InTest.Type) { return true; } return false; } }; #endif // TYPE_Aは削除! #if defined(USE_GLOBALFUNCTION) || defined(USE_LOCALFUNCTOR) vector<Test>::iterator End = remove_if(Tests.begin(), Tests.end(), bind(IsSameType, _1, TYPE_A)); Tests.erase(End, Tests.end()); #endif #if defined(USE_LOCALFUNCTION) vector<Test>::iterator End = remove_if(Tests.begin(), Tests.end(), bind(local::IsSameType, _1, TYPE_A)); Tests.erase(End, Tests.end()); #endif printf("Tests.size() = %d\n",Tests.size()); return 0; }
ちなみにエラーメッセージはこんなの
1>------ ビルド開始: プロジェクト: function_in_function, 構成: Debug Win32 ------ 1>コンパイルしています... 1>main.cpp 1>c:\program files\boost\boost_1_34_1\boost\bind.hpp(66) : error C2039: 'result_type' : 'main::<unnamed-tag>' のメンバではありません。 1> c:\programing\test\vctest\function_in_function\main.cpp(45) : 'main::<unnamed-tag>' の宣言を確認してください。 1> c:\program files\boost\boost_1_34_1\boost\bind\bind_template.hpp(15) : コンパイルされたクラスの テンプレート のインスタンス化 'boost::_bi::result_traits<R,F>' の参照を確認してください 1> with 1> [ 1> R=boost::_bi::unspecified, 1> F= 1> ] 1> c:\programing\test\vctest\function_in_function\main.cpp(69) : コンパイルされたクラスの テンプレート のインスタンス化 'boost::_bi::bind_t<R,F,L>' の参照を確認してください 1> with 1> [ 1> R=boost::_bi::unspecified, 1> F=, 1> L=boost::_bi::list2<boost::arg<1>,boost::_bi::value<TestType>> 1> ] 1>c:\program files\boost\boost_1_34_1\boost\bind.hpp(66) : error C2146: 構文エラー : ';' が、識別子 'type' の前に必要です。 1>c:\program files\boost\boost_1_34_1\boost\bind.hpp(66) : error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません 1>c:\program files\boost\boost_1_34_1\boost\bind.hpp(66) : error C2208: 'boost::_bi::type' : メンバのない列挙型、構造体、共用体が定義されました。 1>c:\program files\boost\boost_1_34_1\boost\bind.hpp(66) : fatal error C1903: 直前のエラーを修復できません。コンパイルを中止します。 1>ビルドログは "file://c:\Programing\test\vctest\function_in_function\Debug\BuildLog.htm" に保存されました。 1>function_in_function - エラー 5、警告 0 ========== ビルド: 0 正常終了、1 失敗、0 更新、0 スキップ ==========