IC_
IC_

Reputation: 1839

SFINAE and decay don't work together. Why?

I'm just learning metaprogramming in C++. I want to make auto detector of opengl type id based on passed type. My code passes decltype(array) which becomes float*. If I use GLType<float>::type it works fine, but GLType<float*>::type fails with "unknown member type" error. I thought std::is_same_v<std::decay_t<T>, float>> will evaluate to true_type on any float*, float[], const float, etc.. as stated here

template <typename T, typename = void>
struct GLType {};

template <typename T>
struct GLType<T, std::enable_if_t<std::is_same_v<std::decay_t<T>, float>>> {
    const static constexpr int type = GL_FLOAT;
};

template <typename T>
struct GLType<T, std::enable_if_t<std::is_same_v<std::decay_t<T>, double>>> {
    const static constexpr int type = GL_DOUBLE;
};

Upvotes: 2

Views: 122

Answers (1)

cigien
cigien

Reputation: 60278

std::decay_t does not remove the pointer from a type. If you want to do that, you need to use std::remove_pointer_t, like this:

template <typename T>
struct GLType<T, std::enable_if_t<std::is_same_v<std::remove_pointer_t<std::decay_t<T>>, float>>> {
                                             //  ^^^^^^^^^^^^^^^^^^^^^  add this   
 const static constexpr int type = GL_FLOAT;
};

and similarly for the other template.

Upvotes: 3

Related Questions