#include struct test1 { void Invoke() {};...">

The error message "template argument is not valid"

Consider the code:

#include <type_traits> #include <iostream> struct test1 { void Invoke() {}; }; struct test2 { template<typename> void Invoke() {}; }; enum class InvokableKind { NOT_INVOKABLE, INVOKABLE_FUNCTION, INVOKABLE_FUNCTION_TEMPLATE }; template<typename Functor, class Enable = void> struct get_invokable_kind { const static InvokableKind value = InvokableKind::NOT_INVOKABLE; }; template<typename Functor> struct get_invokable_kind< Functor, decltype(Functor().Invoke()) > { const static InvokableKind value = InvokableKind::INVOKABLE_FUNCTION; }; template<typename Functor> struct get_invokable_kind< Functor, decltype(Functor().Invoke<void>()) > { const static InvokableKind value = InvokableKind::INVOKABLE_FUNCTION_TEMPLATE; }; int main() { using namespace std; cout << (get_invokable_kind<test1>::value == InvokableKind::INVOKABLE_FUNCTION) << endl; cout << (get_invokable_kind<test2>::value == InvokableKind::INVOKABLE_FUNCTION_TEMPLATE) << endl; } 

What I'm trying to do is create a metafile to test a specific definition of "invokability". And now I'm stuck with this compilation in GCC 4.5.3:

prog.cpp: 37: 3: error: template argument 2 is not valid

What does it mean? Why can I specialize in decltype(Functor().Invoke()) but cannot decltype(Functor().Invoke<void>()) ?

+4
c ++ gcc c ++ 11 sfinae template-meta-programming
source share
1 answer

You need to qualify it with template due to parsing ambiguities:

  decltype(Functor().template Invoke<void>()) ^^^^^^^^ 

Related: Where and why do I need to put the template and typename keywords?

Also consider using the constructor std::declval , rather than Functor() .

+6
source share

All Articles