I have a function that takes a template type to determine a return value. Is there any way to tell at compile time if the template type is some instantiation of a template class?
Ex.
class First { /* ... */ };
template <typename T>
class Second { /* ... */ };
using MyType = boost::variant<First, Second<int>, Second<float>>;
template <typename SecondType>
auto func() -> MyType {
static_assert(/* what goes here?? */, "func() expects Second type");
SecondType obj;
// ...
return obj;
}
MyType obj = func<Second<int>>();
I know it is possible to get around this by doing
template <typename T>
auto func() -> MyType {
static_assert(std::is_same<T, int>::value || std::is_same<T, float>::value,
"func template must be type int or float");
Second<T> obj;
// ...
return obj;
}
MyType obj = func<int>();
I'm just curious in general if there is a way to test if a type is an instantiation of a template class? Because if MyType
ends up having 6 Second
instantiations, I don't want to have to test for all possible types.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…