vendredi 22 décembre 2017

Is it possible to deduce type of non-type template parameter in C++11?

C++17 has this nice feature which allows you to declare non-type template arguments with auto keyword:

template<auto a>
void foo();

which allows this parameters type to be deduced, like that:

foo<3>() <-- a has deduced type of 'int'

Unfortunately, I have to use a compiler which doesn't support C++17 and doesn't fully support C++11 either (it lacks support for standard library). So my question is: is it possible to achieve the same effect using C++11?

The only way that I could come up with is using a macro:

template< typename T, T a>
void foo();

#define CALL_FOO( x ) foo< decltype(x), x >()

Aucun commentaire:

Enregistrer un commentaire