Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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
C++ 类模板中数据成员的条件包含/排除_C++_Templates - Fatal编程技术网

C++ 类模板中数据成员的条件包含/排除

C++ 类模板中数据成员的条件包含/排除,c++,templates,C++,Templates,我想使用SIMD指令和编译器内部函数优化我的向量和矩阵类(确切地说是类模板)。我只想针对元素类型为“float”的情况进行优化。使用SIMD指令需要触摸数据成员。因为我不想为维护两个单独的类而烦恼,所以我希望能够根据模板参数的类型启用/禁用一些数据成员。如果适用,这种方法的另一个优点是,对于我不想编写专门化的函数,我可以使用与一般情况相同的代码。因此,我想在伪代码中实现的是: template< typename T > class Vector3 { if type( T

我想使用SIMD指令和编译器内部函数优化我的向量和矩阵类(确切地说是类模板)。我只想针对元素类型为“float”的情况进行优化。使用SIMD指令需要触摸数据成员。因为我不想为维护两个单独的类而烦恼,所以我希望能够根据模板参数的类型启用/禁用一些数据成员。如果适用,这种方法的另一个优点是,对于我不想编写专门化的函数,我可以使用与一般情况相同的代码。因此,我想在伪代码中实现的是:

template< typename T >
class Vector3 {
    if type( T ) == float:
        union {
            __m128 m128;
            struct {
                float x, y, z, pad;
            };
        };
   else
       T x, y, z;
   endif
};
模板
类向量3{
如果类型(T)=浮动:
联合{
__m128 m128;
结构{
浮动x,y,z,衬垫;
};
};
其他的
tx,y,z;
恩迪夫
};
我知道通过使用Boost.enable_if或类似工具,可以有条件地包含成员函数。但我要寻找的是有条件地包含数据成员。一如既往,我们非常感谢您的帮助。我们也欢迎其他有效的建议


谢谢。

我想到的一个解决方案是部分专门化模板,这是Martin York发布的,但有点扭曲

我建议使用一种特殊的content\u type-struct来提供布局类型,如下所示:

// content for non float types
template<typename T>
struct content_type {
   typedef typename T member_type;
   member_type x,y,z;
   member_type& X { return x; }
   // ...
   // if access to optional members is needed, better use CT_ASSERT or similar
   member_type& Pad { char assert_error_no_pad_here[0]; }
};

// content for float types
struct content_type<float> {
   typedef typename float member_type;
   member_type x, y, z, pad;
   member_type& X { return x; }
   // ...
   member_type& Pad { return pad; }
};

template<typename T>
class Vector3 {
    typedef typename content_type<T> layout_type;
    typedef typename content_type<T>::member_type member_type;

    layout_type _content;

  public:
    member_type& X { return _content.X(); }
    memmber_type& Pad { return _content.Pad(); }
};

// or maybe, if memory layout is not important, just inherit (watch for virtual members)
template<typename T>
class Vector3 : public content_type<T> {
    typedef typename content_type<T> layout_type;
    typedef typename content_type<T>::member_type member_type;
};
//非浮点类型的内容
模板
结构内容类型{
typedef typename T成员类型;
x、y、z型构件;
成员类型&X{return X;}
// ...
//如果需要访问可选成员,最好使用CT_ASSERT或类似工具
成员类型和填充{char assert_error_no_Pad_here[0];}
};
//浮动类型的内容
结构内容类型{
typedef typename浮动成员\ u type;
x型、y型、z型、pad型构件;
成员类型&X{return X;}
// ...
成员类型&Pad{return Pad;}
};
模板
类向量3{
typedef typename内容\类型布局\类型;
typedef typename content_type::member_type member_type;
布局类型内容;
公众:
成员_type&X{return _content.X();}
memmber_type&Pad{return_content.Pad();}
};
//或者,如果内存布局不重要,只需继承(注意虚拟成员)
模板
类向量3:公共内容类型{
typedef typename内容\类型布局\类型;
typedef typename content_type::member_type member_type;
};
优点是只需编写Vector3及其所有逻辑一次


不过,您需要一个较新的编译器来正确地执行此操作(MSVC>7,gcc>3)

非常好。知道我应该如何访问那些不属于公共分母(例如“pad”)的数据成员吗。一种方法是在数据成员不存在的情况下,通过Boost.enable_禁用相应的访问器成员函数。谢谢。我添加了一个解决方案,但是如果boost::enable\u稍微干净一点的话。最佳解决方案取决于您的实际使用情况,要使运行时逻辑尽可能清晰,类型定义需要稍微复杂一点。查看以下讨论: