Creo una classe template e passo la T come argomento di tipo predefinito. Tuttavia questo causa un errore di compilazione. Chiunque può spiegare cosa succede? Grazie!Perché passare T da template esterno come argomento predefinito a std :: function causa errore di compilazione?
PS. Il compilatore che uso è 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;
}
uscite del compilatore:
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
Sembra un bug del compilatore MSVC. Compila bene sotto g ++ 4.7.0. – Yuushi
@Yuushi Oh davvero? Grazie mille. Volevo solo fare una versione predefinita della funzione <> che è equivalente alla funzione. –
Il compilatore trasforma il tuo 'T' in un' void (*)() 'invece di' void() ', quindi lo passa a' std :: function', che non è un tipo valido da passare a 'std: : function'. Probabilmente potresti codificare una soluzione alternativa che prende "A (B ...)" o "A (*) (B ...)" come un argomento e un 'modello classe F' come l'altro e fa 'F 'se davvero volessi risolvere il problema. –
Yakk