Is it possible to use a string literal as a template argument?

Yes, in c++20.

The problem was that determining uniqueness of a template non-type argument was difficult.

c++20 adds in a <=> spaceship operator comparison. If it is non-user provided (and based only off non-user provided <=> in turn, repeat recursively) (and a few other requirements; see p0732), the type can be used as a non-type template argument.

Such types can be constructed from raw "strings" in constexpr constructors, including using c++17 deduction guides to make them auto-size themselves.

As the size of the data stored is probably going to be part of the type, you’ll want to take the type as an auto typed non-type parameter or otherwise auto-deduced type.


Note that placing the implementation of your template in a cpp file is usually a bad idea. But that is another question.

Leave a Comment

tech