c++templatesstring-literalsc++-templatesnon-type-template-parameter

Passing a string literal as a non-type argument to a class template


I want to declare a class template in which one of the template parameters takes a string literal, e.g. my_class<"string">.

Can anyone give me some compilable code which declares a simple class template as described?


Solution

  • Sorry, C++ does not currently support the use of string literals (or real literals) as template parameters.

    But re-reading your question, is that what you are asking? You cannot say:

    foo <"bar"> x;
    

    but you can say

    template <typename T>
    struct foo {
       foo( T t ) {}
    };
    
    foo <const char *> f( "bar" );