c++ - 如何使 _t 版本的 SFINAE 结构公开静态成员值?

标签 c++ templates struct template-meta-programming sfinae

我的代码可以根据 C++ 类型识别您需要使用的 GL 类型。我想制作它的 _t 版本(如 std::decay_tstd::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/

相关文章:

c++ - 错误 : expected nested-name-specifier before 'xxx'

c++ - foreach(int i.. 和 foreach(auto i

bash - 如何使用模板使用 bash 创建 .ini 文件

c++ - OpenCV 模板匹配相似对象

c++ - 前向声明来自不同命名空间的抽象类

不包括 C++ 标准库头文件的 Python setuptools

c++ - 判断一个类是否有某个成员?

c - C 中链表的段错误

带有 golang 属性的 xml 文档

c - C 中的结构指针