我正在使用Visual Studio 11測試版,我對編譯錯誤感到好奇,因爲我在類中存儲了一個std :: function對象。C++ 11 std :: function是否限制函數指針可以擁有的參數個數?
typedef std::function<void (int, const char*, int, int, const char*)> MyCallback;
在我的課堂我有,
MyCallback m_callback;
這編譯就好了。如果我在列表中添加一個參數,則失敗。
typedef std::function<void (int, const char*, int, int, const char*, int)> MyCallback;
故障是:
>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(535): error C2027: use of undefined type 'std::_Get_function_impl<_Tx>'
1> with
1> [
1> _Tx=void (int,const char *,int,int,const char *,int)
1> ]
1> f:\development\projects\applications\my.h(72) : see reference to class template instantiation 'std::function<_Fty>' being compiled
1> with
1> [
1> _Fty=void (int,const char *,int,int,const char *,int)
1> ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(536): error C2504: 'type' : base class undefined
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(539): error C2027: use of undefined type 'std::_Get_function_impl<_Tx>'
1> with
1> [
1> _Tx=void (int,const char *,int,int,const char *,int)
1> ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(539): error C2146: syntax error : missing ';' before identifier '_Mybase'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(539): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
這是準備數據傳遞給另一個應用程序的動態鏈接庫。我當然可以修改數據的格式,以便可以用更少的參數傳遞,但我想知道爲什麼我看到這個限制?
切換回C風格的函數指針,
typedef void (*MyCallback)(int, const char*, int, int, const char*, int);
似乎很好地工作。
正確答案。 STL自己在2012年的GoingNative上也看到了[Magic && Secrets](http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/STL11-Magic-Secrets),他提到了這一次或兩次。 – Xeo 2012-04-10 16:01:18
有趣的事情。我記得實現可變參數模板只是g ++方面的一小部分工作。 – 2012-04-10 16:04:56
非常好,那回答我的問題,謝謝! – BZor 2012-04-10 16:12:14