我創建一個模板類,並將T作爲默認類型參數傳遞。但是這會導致編譯失敗。任何人都可以解釋發生什麼謝謝!爲什麼將T從外部模板作爲默認參數傳遞給std :: function會導致編譯錯誤?
PS。我使用的編譯器是VS2012。
#include <functional>
using namespace std;
template <typename T = void()>
struct delegate
{
typedef function<T> function_t;
function_t f;
};
int main()
{
delegate<> d;
return 0;
}
編譯器輸出:
1>.\Microsoft Visual Studio 11.0\VC\include\functional(554): error C2027: use of undefined type 'std::_Get_function_impl<_Tx>'
1> with
1> [
1> _Tx=void (__cdecl *)(void)
1> ]
1> test.cpp(12) : see reference to class template instantiation 'std::function<_Fty>' being compiled
1> with
1> [
1> _Fty=void (__cdecl *)(void)
1> ]
1> test.cpp(17) : see reference to class template instantiation 'delegate<>' being compiled
1>.\Microsoft Visual Studio 11.0\VC\include\functional(555): error C2504: 'type' : base class undefined
1>.\Microsoft Visual Studio 11.0\VC\include\functional(558): error C2027: use of undefined type 'std::_Get_function_impl<_Tx>'
1> with
1> [
1> _Tx=void (__cdecl *)(void)
1> ]
1>.\Microsoft Visual Studio 11.0\VC\include\functional(558): error C2146: syntax error : missing ';' before identifier '_Mybase'
1>.\Microsoft Visual Studio 11.0\VC\include\functional(558): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
看起來像一個MSVC編譯器錯誤。在g ++ 4.7.0下編譯得很好。 – Yuushi
@Yuushi真的嗎?非常感謝你。我只是想做一個默認版本的函數<>,它相當於函數。 –
編譯器正在將'T'變成void(*)()而不是'void()',然後將'T'傳遞給'std :: function',這不是一個有效的類型傳遞給'std: :function'。你或許可以編寫一個解決方案,它可以將'A(B ...)'或'A(*)(B ...)'作爲一個參數,並將一個'template class F'作爲另一個,並且'F 「如果你真的想解決這個問題。 –
Yakk