类模板内数据成员的条件包含/排除

编程入门 行业动态 更新时间:2024-10-26 00:28:09
本文介绍了类模板内数据成员的条件包含/排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想使用 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 06:17:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:成员   模板   条件   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!