How to set a template template parameter with a numeric value?

I want the template parameter to accept a template with a numeric template argument.

This example may be too simplistic, but I would like something like this:

template <int X> struct XX { static const int x = X; }; template<typename TT, TT V, template<V> TX> void fnx(TX<V> x) { static_assert(V == TX::x, "IMPOSSIBLE!"); } void fny() { fnx(XX<1>()) } 

I should not understand the syntax of this, as it should be possible. How to do it?

+6
source share
2 answers

Just fix your syntax a bit - since the template template parameter is set incorrectly, we get something like this:

 template <typename T, template <T > class Z, T Value> // ^^^^^^^^^^^^^^^^^^^^^ void foo(Z<Value> x) { } 

However, the compiler cannot output T here - this is not an inferred context. You must explicitly provide it:

 foo<int>(XX<1>{}); 

This is pretty annoying. I can't even write a type trait so that non_type_t<XX<1>> an int (where this type indicates the actual introspection of the type, and not something that int trivially returns).


There is a proposal to improve this process ( P0127 ) by amending the non-deducible context of non-piggy type template arguments,

+6
source

Your fnx ad needs some work, and the TT type cannot be displayed on the call site.

 template<typename TT, TT V, template<TT> class TX> void fnx(TX<V> x) { static_assert(V == TX<V>::x, "IMPOSSIBLE!"); } void fny() { fnx<int>(XX<1>()); } 

Working example: https://ideone.com/57PsCA

+2
source

All Articles