How do I static_assert
like this? Maybe Boost supports it if not C++ or new features in C++11?
template
struct foo {};
template
You could do something along these lines. Given a trait that can verify whether a class is an instantiation of a class template:
#include
template class TT>
struct is_instantiation_of : std::false_type { };
template class TT>
struct is_instantiation_of, TT> : std::true_type { };
Use it as follows in your program:
template
struct foo {};
template
struct bar {
static_assert(is_instantiation_of::value, "failure");
};
int main()
{
bar b; // ERROR!
bar> b; // OK!
}
If you want, you could generalize this to detect whether a class is an instance of a template with any number of (type) parameters, like so:
#include
template class TT, typename T>
struct is_instantiation_of : std::false_type { };
template class TT, typename... Ts>
struct is_instantiation_of> : std::true_type { };
template
struct bar {
static_assert(is_instantiation_of::value, "failure");
};
You would then use it this way in your program:
template
struct bar {
static_assert(is_instantiation_of::value, "failure");
};
int main()
{
bar b; // ERROR!
bar> b; // OK!
}
Here is a live example.