Home > Blockchain >  syntax variants for function pointer as non type template arg
syntax variants for function pointer as non type template arg

Time:11-20

I want to use a function pointer as non type template parameter. I can do it with a predefined alias to the function pointer type with typedef or using. Is there any syntax for the template definition without a predefined type alias?

bool func(int a, int b) { return a==b; }

//using FUNC_PTR_T = bool(*)(int,int); // OK
typedef bool(*FUNC_PTR_T)(int,int); // also OK

template < FUNC_PTR_T ptr >
void Check()
{
    std::cout << ptr( 1,1 ) << std::endl;
}

int main()
{
    Check<func>();
}

I want to write in in a single step like:

// this syntax did not compile...
template < bool(*)(int,int) ptr >
void Check() 
{
    ptr(1,1);
}

Can someone point me top a valid syntax for that?

CodePudding user response:

You are going to declare a non-type template parameter. You can write

template < bool( *ptr )( int, int ) >
void Check()
{
    ptr( 1, 1 );
}
  • Related