Reputation: 13162
I implemented the following class:
template<typename Enum>
class EnumDescription
{
//TODO Force Enum to be an enum type
public:
static std::string get( const Enum val );
};
This works as expected, but now I would like to constraint the Enum template parameter to be an enum/enum class only.
Upvotes: 5
Views: 3059
Reputation: 170065
Depends on your flavor of C++.
C++17
The most easy to write way is a static assertion
template<typename Enum>
class EnumDescription
{
static_assert(std::is_enum_v<Enum>);
public:
static std::string get( const Enum val );
};
C++20
You can specify an ad-hoc constraint on the template
template<typename Enum> requires std::is_enum_v<Enum>
class EnumDescription
{
public:
static std::string get( const Enum val );
};
or, probably for the better, with a reusable concept
template<typename E>
concept EnumType = std::is_enum_v<E>;
template<EnumType Enum>
class EnumDescription
{
public:
static std::string get( const Enum val );
};
Upvotes: 10