Often (most of the time?) when creating types with template type parameters you want to ensure the type parameter is filled in with a non-reference, unqualified (non-const, non-volatile) type. However, a simple definition like the following lets the user fill in any type for T:
template <typename T>
class MyContainer {
T* whatever;
T moreStuff;
};
Modern C++ has concepts that should be able to take care of this problem. What is the best (and preferably least boilerplate-y) way to do this?
static_assert is a good option:
#include <type_traits>
template <typename T>
class MyContainer
{
static_assert(!std::is_reference_v<T>);
static_assert(!std::is_const_v<T>);
static_assert(!std::is_volatile_v<T>);
T* whatever;
T moreStuff;
};
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