我只是在学习C ++中的元编程。我想基于传递的类型使opengl类型id的自动检测器。我的代码通过了decltype(array)
,成为float*
。如果我使用GLType<float>::type
它,效果很好,但GLType<float*>::type
失败并显示“未知成员类型”错误。我想std::is_same_v<std::decay_t<T>, float>>
将计算为true_type
在任何float*, float[], const float, etc..
的说明这里
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;
};
std::decay_t
不会从类型中删除指针。如果要这样做,则需要使用std::remove_pointer_t
,例如:
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;
};
和其他模板类似。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句