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?
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)) {}
// ...
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.