我想为类型定义一个
这是因为我希望能够获取它们中的两个(类型相同)的插值值,例如:
template<T>
,但我必须确保只有定义了operator/
和operator+
的类型才能被传递给T
。这是因为我希望能够获取它们中的两个(类型相同)的插值值,例如:
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!
对于简单类型如float
,int
等,没问题。但如果使用复杂类型T
且它们没有定义operator/
和operator+
,该怎么办呢?