在编译时动态生成结构体 [英] Generating Structures dynamically at compile time

查看:193
本文介绍了在编译时动态生成结构体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须生成一个数据结构,它只在某些条件下包含某些字段。这通常总是翻译成如下

  struct MyStruct {
int alwaysHere;

#ifdef WHATEVER
bool mightBeHere;
#endif

char somethingElse;

#if SOME_CONSTANT> SOME_VALUE
uint8_t alywasHereButDifferentSize;
#else
uint16_t alywasHereButDifferentSize;
#endif
...
};

从我的角度来看,这很容易看起来难看,
甚至不谈论处理这些字段的代码,通常在
ifdefs下。



我在寻找一种优雅的方式来实现相同的结果,而不添加任何开销,但与一个代码多
更加可读。模板专业化似乎有点过分,但在我看来,
是唯一的替代方案。



C ++ 11添加任何东西来处理这个

解决方案

任何建议都可以使用。第二种情况,我通常喜欢一个typedef限制黑客到一个地方:

  #if SOME_CONSTANT> SOME_VALUE 
typedef uint8_t always_type;
#else
typedef uint16_t always_type;
#endif

然后剩下的代码将使用 always_type 整个:

  struct MyStruct {
// ...
always_type always_here_but_different_size;
// ...
};

如果您要使用:

  typedef std :: conditional<(SOME_CONSTANT> VALUE),uint8_t,uint16_t> :: type always_type; 

这也很好,这里的重点不是你使用的语法想要,但事实上,你通常要为该类型创建一个名称,所以你可以在需要的地方使用它。



对于某种东西存在或不存在的情况,这是一个很难说。通常,这样的事情将涉及在构建时间启用/禁用某些特征。如果是这样,看起来该类具有与可以被启用/禁用的特征相关的职责,以及其他事物。这听起来像是可能违反单一责任原则,可能不是非常有凝聚力。如果是这样,它可能表明一个问题,更好地解决在整体设计的水平,而不仅仅是你使用的语法。



注意:我可能是外推相当多的从可信的最小的证据 - 可能超过证据真正支持。


I have to generate a data structure that contains certain fields only under certain condition. This typically always translates to something like the following

struct MyStruct {
    int     alwaysHere;

#ifdef WHATEVER
    bool    mightBeHere;
#endif

    char    somethingElse;

#if SOME_CONSTANT > SOME_VALUE
    uint8_t alywasHereButDifferentSize;
#else
    uint16_t alywasHereButDifferentSize;
#endif
...
};

From my point of view this gets easily ugly to look at, and unreadable. Without even talking about the code that handle those fields, usually under ifdefs too.

I'm looking for an elegant way to achieve the same result without adding any overhead whatsoever, but with a code much more readable. Template specialization seems a bit excessive, but it seems to me to be the only alternative.

Is C++11 adding anything at all to deal with this situation?

Any suggestion would be appreciated.

解决方案

For the second case, I'd usually prefer a typedef that restricts the hackery to one place:

#if SOME_CONSTANT > SOME_VALUE
    typedef uint8_t always_type;
#else
    typedef uint16_t always_type;
#endif

Then the rest of your code will just use always_type throughout:

struct MyStruct {
    // ...
    always_type always_here_but_different_size;
    // ...
};

If you want to use:

typedef std::conditional<(SOME_CONSTANT > VALUE), uint8_t, uint16_t>::type always_type;

That's fine too -- the point here isn't about the syntax you use to get the type you want, but the fact that you generally want to create a name for that type so you can use it where needed.

As for the situation of something being present or not, it's a little hard to say. Typically, such a thing will relate to enabling/disabling certain features at build time. If so, it appears that the class has responsibilities related both to the feature(s) that can be enabled/disabled, and to something else as well. That sounds like it's probably violating the single responsibility principle, and may not be very cohesive. If that's the case, it may indicate a problem that's better addressed at the level of the overall design, than simply the syntax you use.

Caveat: I'm probably extrapolating quite a bit from admittedly minimal evidence -- possibly more than the evidence really supports.

这篇关于在编译时动态生成结构体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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