Is there a possibility to have a static assertion whether a type provided as template argument implements all of the types listed in the parameter pack ie. a parameter pack aware std::is_base_of()?
template <typename Type, typename... Requirements>
class CommonBase
{
static_assert(is_base_of<Requirements..., Type>::value, "Invalid.");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
parameter pack aware version of std::is_base_of()
public:
template <typename T> T* as()
{
static_assert(std::is_base_of<Requirements..., T>::value, "Invalid.");
return reinterpret_cast<T*>(this);
}
};
Update for C++17: With C++17's fold expressions this becomes almost trivial:
Original Answer (C++11/14): You might use pack expansion and some static version of
std::all_of
:The pack expansion will expand to the list of values you get by inserting every type in
Requirements...
for the question mark instd::is_base_of<Type, ?>::value
, i.e. for the first line in main it will expand tostatic_all_of<true, true>
, for the second line it will bestatic_all_of<true, false, true>
Just for future reference, because I just had this problem, with C++17 you can now use fold expressions like this: