TwilCynder
TwilCynder

Reputation: 33

C++ : define struct/class template differenty for class and non-class types

in my C++ projet, I use a simple struct template with one template argument (a Vec2, with x and y attributes), and I need to define it differently for two main use cases :

Which brings me to the question : how can i (using C++17) make two different definitions of my class, one for class types and one for others ? Can I make some kind of partial specialization for just the class types ?

Upvotes: 0

Views: 57

Answers (1)

Jarod42
Jarod42

Reputation: 217135

You can add extra parameter to enable SFINAE, something like

template <typename T, typename Enabler = void>
struct Vec2
{
    T x;
    T y;
    // ...
};

template <typename T>
struct Vec2<T, std::enable_if_t<std::is_class_v<T>>>
{
private:
    T x;
    T y;

public:
    Vec2(T x, T y) : x(std::move(x)), y(std::move(y)) {}
    // ...
};

C++20 would allow specialization with concepts instead.

Upvotes: 1

Related Questions