fredrik.eriksson

Coffee and a keyboard

C++ string template argument

There is a pretty cool and sometimes useful way to pass strings as a template argument as shown in the example bellow:

#include <iostream>

template<const char* name>
class foo
{
    const char* val;
public:
    foo(): val( name ) { }
    const char* value( void ) { return val; }
};

extern const char exname[] = "hello world";

int main(int, char** )
{
    foo< exname > myfoo;
    std::cout << myfoo.value() << std::endl;
}