Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template specialization for two types

I have a static member function lerp() inside my class template AnimCurve which I want to specialize for Quaternions, this way:

template<>
inline Quatf AnimCurve<Quatf>::lerp( 
    const Quatf& start, 
    const Quatf& end, 
    float time 
    )
{
    return start.slerp(time, end);
}

However, this is not generic enough because one may also use Quatd. Is it possible to write a function which would work for both, since both Quatf and Quatd are type definitions of Quaternion<T>?

Here is the current definition of AnimCurve:

template< typename T >
class AnimCurve {
public:
    AnimCurve() {}
    void addKeyframe(float time, T value);
    T getvalue(float time) const;
private:
    static inline T lerp( const T& start, const T& end, float time );
    std::map<float, T> mKeyframes;
};
like image 791
num3ric Avatar asked Jul 12 '26 06:07

num3ric


1 Answers

Is it possible to write a function which would work for both, since both Quatf and Quatd are type definitions of Quaternion?

If you want to specialize your lerp algorithm for exactly those two instantiations of the Quaternion class template, and no other instantiations, then no, you have to explicitly specialize both of them: once for AnimCurve<Quatf> and once for AnimCurve<Quatd>.

like image 78
Andy Prowl Avatar answered Jul 14 '26 20:07

Andy Prowl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!