Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type traits for both `type` and `const type`

Tags:

c++

templates

I'm in a need for some sort of a type trait:

template<typename T> struct foo {};
template<>
struct foo<char> { static constexpr char c = 'c' };

This works perfectly if I need a character c of the type char, but doesn't in the following case:

printf("%c", foo<const char>::c);

Is there a more elegant way to do this, rather than specify the template for both char and const char in the same way?

like image 896
Jytug Avatar asked Dec 19 '22 11:12

Jytug


1 Answers

Add a partial specialization:

template <class T> struct foo<const T> : foo<T> {};
like image 80
T.C. Avatar answered Dec 24 '22 02:12

T.C.