C++ – How to take a pointer to a template function specialized on a string


I was trying use a set of filter functions to run the appropriate routine, based on a string input. I tried to create matcher functions for common cases using templates, but I get a "type not equal to type" error when I try to store a pointer to the specialized function (in a structure, in the real application)

Distilled example from a Visual C++ 8 'console application'

template <const char *C>
const char*
f(void) {
  return C;

const char* (*g)(void) = f<"hi">;

int _tmain(int argc, _TCHAR* argv[])
  return g();

This fails with the error

Error   1   error C2440: 'initializing' : cannot convert from 'const char *(__cdecl *)(void)' to 'const char *(__cdecl *)(void)'    c:\files\pointer.cpp    7   

(It also has an error on the main return value, but that doesn't concern me here.)

The same example succedes if const char * is replaced with int.

Best Solution

Strings as template-value parameters are prohibited by the ISO standard.

Related Question