使用C ++模板编译类成员变量? [英] Compiling Class Member Variables Out with C++ Templates?

查看:205
本文介绍了使用C ++模板编译类成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的类:

I have a class that looks similar to this:

class Compound
{
    void* pValue0;
    void* pValue1;
    void* pValue2;
    void* pValue3;
    void* pValue4;
    void* pValue5;
    void* pValue6;
    void* pValue7;
    void* pValue8;
    void* pValue9;
    void* pValueA;
    void* pValueB;
    void* pValueC;
};

当我创建一个新的复合类时,我分配额外的内存[sizeof(Compound)+ extraSpace]。每个pValue指向额外内存中的地址。

When I create a new Compound class, I allocate extra memory [sizeof(Compound) + extraSpace]. Each of the pValue's refer to an address in the extra memory.

现在,我想减少pValue的数量,取决于我需要的。

Now, I would like to reduce the number of pValue's depending on which of them I need. Templates seem like a good fit.

所以,如果我想要一个化合物< 0A>,我只需要pValue0和pValueA,然后让编译器删除所有其他pValue 。基本上,我希望它变成:

So if I wanted a class Compound<0A>, I would only want pValue0 and pValueA, and then have the compiler remove all other pValues. Essentially, I would want it to then become:

template <uint Mask = 0A>
class Compound<Mask>
{
    void* pValue0;
    void* pValueA;
}

这是可能吗?我接近enable_if,但是当我试图限制它到一个特定的掩码,编译器抛出的错误,当enable_if case是false时无法找到一个类型。

Is this possible? I got close with enable_if, but when I tried to limit it to a particular mask the compiler threw errors about being unable to find a type when the enable_if case was false.

推荐答案

这可能会:

template<char...>
struct flags_tag {constexpr flags_tag(){}; };

template<char...Cs>
struct make_flags{ using type=flags_tag<Cs...>; };
template<char...Cs>
struct make_flags<'0','x',Cs...>:make_flags<Cs...>{};
template<char...Cs>
struct make_flags<'0','X',Cs...>:make_flags<Cs...>{};
template<char...Cs>
using make_flags_t = typename make_flags<Cs...>::type;

template<char...Cs>
constexpr make_flags_t<Cs...> operator""_flag(){ return {}; }

template<char> struct pValue_t;
template<> struct pValue_t<'0'>{ void* pValue0 = 0; };
template<> struct pValue_t<'1'>{ void* pValue1 = 0; };
// ...
template<> struct pValue_t<'A'>{ void* pValueA = 0; };
template<> struct pValue_t<'B'>{ void* pValueB = 0; };
template<> struct pValue_t<'C'>{ void* pValueC = 0; };

template<class flags>
struct Compound;

template<char...Cs>
struct Compound< flags_tag<Cs...> >:
  pValue_t<Cs>...
{};

然后你使用它:

using my_type = Compound< decltype( 0x0A_flag ) >;
int main() {
  my_type test;
  std::cout << test.pValue0 << test.pValueA << '\n';
}

这似乎是你想要的。

我也禁用你的复合类型的复制/移动ctor,并使其他构造函数 private

I'd also disable the copy/move ctor of your Compound type, and make its other constructors private with a friend factory function.

请注意,此代码可以生成指数数量的类(例如, 2 ^ 12,或4k),并且可以导致二进制膨胀(如果任何每类代码不内联的存在)。

Note that this code can generate an exponential number of classes (2^12, or 4k), and that can cause binary bloat (if any per-class code isn't inlined out of existence).

[live example]

[live example]

这篇关于使用C ++模板编译类成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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