how could I force the static_assert
in this given class:
template < int I >
struct foo
{
static_assert( I < 5 ,"I must be smaller than 5!" );
};
to fire when I instantiate the template no when I instantiate the resulting type:
int main()
{
typedef foo< 5 > t; // compiles
t tt; // will not compile
}
One suggestion
template <int I>
struct foo_guard {
static_assert(I < 5, "I must be smaller than 5!");
typedef void type;
};
template < int I, typename = typename foo_guard<I>::type>
struct foo
{
};
There might be a more elegant way, but you could make foo
a metafunction that refers to itself:
template < int I >
struct foo
{
static_assert( I < 5 ,"I must be smaller than 5!" );
typedef foo<I> type;
};
int main()
{
typedef typename foo< 5 >::type t; // won't compile
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With