Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

syntax variants for function pointer as non type template arg

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

template < bool( *ptr )( int, int ) >
void Check()
{
    ptr( 1, 1 );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading