我的代码可以根据 C++ 类型识别您需要使用的 GL 类型。我想制作它的 _t
版本(如 std::decay_t
或 std::enable_if_t
)但公开 int
常量值
template <typename T, typename = void> struct GLType {};
template <typename T>
struct GLType<T, std::enable_if_t<std::is_same_v<std::remove_pointer_t<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::remove_pointer_t<std::decay_t<T>>, double>>> {
const static constexpr int type = GL_DOUBLE;
};
我的第一次尝试是
template <typename T>
using GLType_t = GLType<T>::type;
但这行不通。甚至可以以相同的方式返回值而不是类型吗?
最后,我想要类似的东西
int a = GLType_t<float>;
// instead of
int a = GLType<float>::type; // which works fine btw
最佳答案
您似乎在寻找 variable-templates这允许你这样做:
template <typename T>
inline constexpr int GLType_t = GLType<T>::type;
然后你可以像这样使用它:
int a = GLType_t<float>;
此外,我强烈建议您将 int
成员命名为 value
而不是 type
。名称很重要,type
只是成员的错误名称,它实际上不是类型。
关于c++ - 如何使 _t 版本的 SFINAE 结构公开静态成员值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63197040/