Create template <T> only for such T that has operator / defined
I would like to define a type template<T>
for a type, but I have to make sure that only types that have operator/
and operator+
can be passed asT
.
This is because I want to be able to get the interpolated value for two of them (of the same type), for example:
template<class T>
class Key final : public KeyBase{
public: //to keep the example simple
//unsigned timeMS; <-- iherited from KeyBase
T value;
public:
Key(unsigned timeMS, const T &value)
: KeyBase(timeMS), value(value){}
T inline getValue(){ return value; }
};
Key<float> start = {10, 5.0f};
Key<float> end = {15, 3.0f};
float ratioAtTime12 = 12 / (end.timeMS - start.timeMS);
float valueAtTime12 = start.value + (end.value - start.value) * ratio;
//Point2D is my own custom type that have operator+, - and / defined
Key<Point2D> start2 = {10, Point2D(10, 15)};
Key<Point2D> end2 = {15, Point2D(111, 6)};
...
Key<Character> start3 = {10, Character("Alan")}; //SHOULD generate an error
//because my custom Character type has no operator+, - or / defined!
For simple types such as float
, int
etc., this is fine. But how to prevent the use of complex types such as T
, if they do not have operator/
and operator+
certain ?
source to share
If you need a good error message that doesn't include douzens operators found via ADL or the like, you can define traits:
template <typename, typename=void>
struct isAddable : std::false_type {};
template <typename T>
struct isAddable<T, decltype(void(std::declval<T>() + std::declval<T>()))>
: std::true_type {};
template <typename, typename=void>
struct isDividable : std::false_type {};
template <typename T>
struct isDividable<T, decltype(void(std::declval<T>() / std::declval<T>()))>
: std::true_type {};
... and use a static assertion.
static_assert( isAddable<T>{} && isDividable<T>{}, "Invalid T!" );
Or, to get more specific information:
static_assert( isAddable<T>{}, "T not addable!" );
static_assert( isDividable<T>{}, "T not dividable!" );
Demo .
You can also use a handy macro to identify these traits.
#define VAL std::declval<T>()
#define DEF_TRAIT(name, expression) \
template <typename, typename=void> struct name : std::false_type {}; \
template <typename T> \
struct name<T, decltype(void(expression))> : std::true_type {};
DEF_TRAIT(isDividable, VAL / VAL)
DEF_TRAIT(isAddable, VAL + VAL)
Demo .
source to share