lundi 31 août 2015

I can't seem to instantiate class specializations with enable_if

I'm trying to make a class that has 3 possible versions of a template method implementation, depending on the template type being of one of three 'type sets'. Also, I'm trying to keep instances of these objects in a map.

So my initial attempt was:

class DescriptorType {
public:
    int id;
    DescriptorType() : id(-1) {}
    virtual ~DescriptorType() {}
};

template <typename T>
class Descriptor : public DescriptorType {
public:
    T value;

    void update(TYPE_CONSTRAINT((TYPE(int) || TYPE(float)))) {
        // specific implementation for these types
    }

    void update(TYPE_CONSTRAINT((TYPE(vec2) || TYPE(vec3) || TYPE(vec4)))) {
        // specific implementation for these types
    }

    void update(TYPE_CONSTRAINT((TYPE(mat2) || TYPE(mat3) || TYPE(mat4)))) {
        // specific implementation for these types
    }
};

(I have an include file with the following -- for the macros):

template <bool B, typename T = void>
using enable_if_t = typename std::enable_if<B, T>::type;

#define TYPE_CONSTRAINT(x) enable_if_t<x, T>
#define TYPE(x) std::is_same<x, T>::value

But it doesn't seem to work. It compiles but the minute I try to instantiate a Descriptor<T> for any T, be it the generic T or a concrete type like 'int' I get linker errors for all possible types (16 in my case).

My guess is I'm using enable_if wrong. But I'm not sure how.

Aucun commentaire:

Enregistrer un commentaire