类模板内数据成员的条件包含/排除 [英] Conditional Inclusion/Exclusion of Data Members Inside Class Templates

查看:16
本文介绍了类模板内数据成员的条件包含/排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 SIMD 指令和编译器内在函数优化我的 Vector 和 Matrix 类(确切地说是类模板).我只想针对元素类型为浮动"的情况进行优化.使用 SIMD 指令需要接触数据成员.由于我不想为维护两个单独的类而烦恼,我希望能够根据模板参数的类型启用/禁用某些数据成员.这种方法的另一个优点是,如果它适用,我可以使用与一般情况相同的代码来处理我不想为其编写专门化的函数.因此,我想用伪代码实现的是:

I want to optimize my Vector and Matrix classes (which are class templates to be exact) using SIMD instructions and compiler intrinsics. I only want to optimize for the case where the element type is "float". Using SIMD instructions requires touching the data members. Since I don't want to be bothered with the trouble of maintaining two separate classes, I want to be able to enable/disable some data members based on the type of the template parameter. Another advantage of this approach, in case it's applicable, is that I can use the same code from the general case for functions that I don't want to write a specialization for. Therefore, what I want to achieve in pseudo code is:

template< typename T >
class Vector3 {
    if type( T ) == float:
        union {
            __m128 m128;
            struct {
                float x, y, z, pad;
            };
        };
   else
       T x, y, z;
   endif
};

我知道通过使用 Boost.enable_if 或类似工具可以有条件地包含成员函数.不过,我正在寻找的是有条件地包含数据成员.与往常一样,非常感谢您的帮助.也欢迎其他有效建议.

I know conditional inclusion of members functions is possible via the use of Boost.enable_if or similar facilities. What I'm looking for though, is conditional inclusion of data members. As always, your help is very much appreciated. Other valid suggestions are also welcome.

谢谢.

推荐答案

我想到的一个解决方案是部分专门化的模板,这是 Martin York 发布的内容,但有所不同.

One solution that springs to mind is partially specialized templates, which is what Martin York posted, but with a twist.

我会推荐一个特殊的 content_type-struct 来提供布局类型,如下所示:

I would recommend a special content_type-struct to supply the layout type, like so:

// 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;
};

优点是您只需编写一次 Vector3 及其所有逻辑.

The advantage is you only have to write Vector3 with all of its logic once.

您需要一个适中的最新编译器才能正确执行此操作 (MSVC>7, gcc>3)

You need a moderately recent compiler to do that correctly, though (MSVC>7, gcc>3)

这篇关于类模板内数据成员的条件包含/排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆