为什么将T从外部模板作为默认参数传递给std :: function会导致编译错误?

Xin*_*ang 7 c++ templates visual-c++ c++11 visual-studio-2012

我创建了一个模板类,并将T作为默认类型参数传递.但是这会导致编译失败.谁能解释会发生什么?谢谢!

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;
}
Run Code Online (Sandbox Code Playgroud)

编译器输出:

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
Run Code Online (Sandbox Code Playgroud)

Quu*_*one 2

正如对您的问题的评论所示:这只是 Visual Studio 中的一个错误,您的 C++ 代码没有任何问题。@Stephan-T-Lavavej 说他已将其归档为 DevDiv#671343。

假设@Yakk的诊断是正确的(MSVC错误地处理Tvoid(*)()而不是void()),我之前提出了“可能的解决方法”

typedef function<typename remove_pointer<T>::type> function_t;
Run Code Online (Sandbox Code Playgroud)

但正如@JoshPeterson 在下面评论的那样,即使进行了更改,该错误仍然出现在 VS2013 中,因此它实际上并不是一个解决方法。