A common pattern with templated classes is that template argument is typedef’ed inside the class to be easily accessible:
#include <type_traits>
template<class T> struct Foo{
typedef T type;
};
static_assert(std::is_same<Foo<int>::type,int>::value,"");
How can I do the same with non-type template argument? I only had the following idea, but there gotta be something more elegant?
template<int I> struct Bar{
constexpr static int getI(){ return I; }
};
static_assert(Bar<5>::getI()==5,"error");
I might use an
enum, but the utility of this seems somewhat limited to me…Edit to include that this kind of thing is frequently done in template metaprogramming.