Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使SFINAE结构的_t版本公开静态成员值? < >我有代码,它可以识别基于C++类型需要使用的GL类型。我想制作一个\u t版本(如std::decay\u t或std::enable\u if\u 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; }; 模板结构GLType{}; 模板 结构类型{ const static constexpr int type=GL_FLOAT; }; 模板 结构类型{ const static constexpr int type=GL_DOUBLE; };_C++_Templates_Struct_Template Meta Programming_Sfinae_C++17 - Fatal编程技术网

如何使SFINAE结构的_t版本公开静态成员值? < >我有代码,它可以识别基于C++类型需要使用的GL类型。我想制作一个\u t版本(如std::decay\u t或std::enable\u if\u 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; }; 模板结构GLType{}; 模板 结构类型{ const static constexpr int type=GL_FLOAT; }; 模板 结构类型{ const static constexpr int type=GL_DOUBLE; };

如何使SFINAE结构的_t版本公开静态成员值? < >我有代码,它可以识别基于C++类型需要使用的GL类型。我想制作一个\u t版本(如std::decay\u t或std::enable\u if\u 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; }; 模板结构GLType{}; 模板 结构类型{ const static constexpr int type=GL_FLOAT; }; 模板 结构类型{ const static constexpr int type=GL_DOUBLE; };,c++,templates,struct,template-meta-programming,sfinae,c++17,C++,Templates,Struct,Template Meta Programming,Sfinae,C++17,我的第一次尝试是 template <typename T> using GLType_t = GLType<T>::type; 模板 使用GLType\u t=GLType::type; 但这不起作用。甚至可以用同样的方式返回值而不是键入吗? 最后,我想要像这样的东西 int a = GLType_t<float>; // instead of int a = GLType<float>::type; // which works fine

我的第一次尝试是

template <typename T>
using GLType_t = GLType<T>::type;
模板
使用GLType\u t=GLType::type;
但这不起作用。甚至可以用同样的方式返回值而不是键入吗?
最后,我想要像这样的东西

int a = GLType_t<float>;
// instead of
int a = GLType<float>::type; // which works fine btw
int a=GLType\u t;
//而不是
int a=GLType::type;//顺便说一句,哪个很好

您似乎正在寻找允许您执行此操作的:

template <typename T>
inline constexpr int GLType_t = GLType<T>::type;
模板
内联constexpr int GLType\u t=GLType::type;
然后你可以这样使用它:

int a = GLType_t<float>;
int a=GLType\u t;
另外,我强烈建议您将
int
成员命名为
value
而不是
type
。名称很重要,
type
对于一个实际上不是类型的成员来说只是一个错误的名称。

提供了相应的方法

然而,我想提出一种使用s的更少类型的方法。整个boiler plate traits代码将崩溃为一个简单的模板函数:

#include <type_traits>

template <typename T>
constexpr auto GLTypeHelper() noexcept
{
   // assert is the T is not either float or double
   static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, " T should be float or double");
   
   if constexpr (std::is_same_v<T, float>) 
      return GL_FLOAT;
   else if constexpr (std::is_same_v<T, double>) 
      return GL_DOUBLE;
};

// variable template for GLType_v
template <typename T>
inline constexpr int GLType_v = GLTypeHelper<T>(); // calls the `GLTypeHelper()`
#包括
模板
constexpr auto GLTypeHelper()无异常
{
//断言是T不是浮点或双精度
静态断言(std::is_same_v | | std::is_same_v,“T应为浮点或双精度”);
如果constexpr(std::is_same_v)
返回总账浮点数;
否则,如果constexpr(std::is_same_v)
返回GL_-DOUBLE;
};
//GLType_v的变量模板
模板
inline constexpr int GLType_v=GLTypeHelper();//调用'GLTypeHelper()`
你会像这样使用它

constexpr int a = GLType_v<float>;
constexpr int b = GLType_v<double>;
constexpr int a=GLType\u v;
constexpr int b=GLType_v;

当然,由于
GL\u FLOAT
GL\u DOUBLE
是值,而不是类型,所以更改
::type
->
::value
,和
\t
->
\v

谢谢,这正是我想要的。是的,在这种情况下,代码> >值>代码>比<代码> > <代码>更有意义:我可以推荐<代码> GLType <代码> > >代码> GLytEtgA<代码>,<代码> GLytEtAG::类型< /COD> >代码> GLytEtAG::值< /COD>,和<代码> GLyType T/<代码> > <代码> GLYTYPE V,最后两个符合C++约定。(请参见
)由于C++17,
内联constexpr int GLType_t=…
。根据POSIX标准,以_t结尾的名称是为实现保留的,因此如果您针对的是POSIX系统(例如Linux),则不应以_t结尾。@jesperjuhl感谢您提供的信息,但无论如何我将其更改为_v