简体   繁体   English

C ++ 11:条件编译:成员

[英]C++11: conditional compilation: members

I'm trying to make a struct with conditional members, that means, different members exists only with specific specializations. 我正在尝试使用条件成员创建一个结构,这意味着,不同的成员只存在特定的特殊化。 But, I want that this classes are fastest as possible. 但是,我希望这些课程尽可能快。 I've tried it in three differents ways: 我试过三种不同的方式:

Way 1: 方式1:

 template<typename T, bool with_int = false>
 struct foo
 {
     template<typename... Args>
     foo(Args&&... args) : m_t(forward<Args>(args)...)
     {}

     T m_t;
 }

 template<typename T>
 struct foo<T, true>
 {
     template<typename... Args>
     foo(Args&&... args) : m_t(forward<Args>(args)...), m_id(0)
     {}

      T m_t;
      int m_id;
 };
  • Disadventage: repeated code for each specialization. Disadventage:每个专业化的重复代码。

Way 2: 方式2:

 template<typename T, bool with_int = false>
 struct foo
 {
     template<typename... Args>
     foo(Args&&... args) : m_t(forward<Args>(args)...)
     {}

     virtual ~foo() {}

     T m_t;
 }

 template<typename T>
 struct foo<T, false> : public foo<T>
 {
      using foo<T>::foo;

      int m_id = 0;
 };
  • Adventage: few code. Adventage:代码很少。
  • Disadventage: Use of vtables/inheritance/etc: more time in construction or access to members? Disadventage:使用vtables /继承/等:更多的时间在构建或访问成员? But, in other way, I don't pretend to to use "references" to base class. 但是,换句话说,我不假装使用“引用”来基类。 What is it the real adventages or disadventages of this aproach? 这种方法的真正冒险或不满是什么?

Way 3 方式3

 using nil_type = void*;
 using zero_type = nil_type[0];

 template<typename T, bool with_int = false>
 struct foo
 {
    template<typename... Args, typename = typename enable_if<with_int>::type>
    foo(Args&&... args) : m_t(forward<Args>(args)...), m_int(0)
    {}

    template<typename... Args, typename = typename enable_if<!with_int>::type>
    foo(Args&&... args) : m_t(forward<Args>(args)...)
    {}        

    T m__t;
    typename conditional<with_int, int, zero_type>::type m_int;
 };
  • Ventages: Write code once; Ventages:写一次代码; when with_int is false , field m_int has size 0 (almost with gcc 4.7.2). with_intfalse ,字段m_int大小为0(几乎与gcc 4.7.2)。
  • Adventages: More use of templates (reduce readability) and I'm not sure about how compilers deal with member of size 0. I don't know indeed to what extent a size-0-field is dangerous or has sense. Adventages:更多地使用模板(降低可读性)并且我不确定编译器如何处理大小为0的成员。我不确定大小0字段在多大程度上是危险的或有意义的。 Repeated constructors, but perhaps this is avoidable. 重复的构造函数,但也许这是可以避免的。

What is the best approach or method? 什么是最好的方法或方法?

Have you considered inheritance? 你考虑过继承吗?

template< bool >
struct foo_int_base
{
  // stuff without the int
  void f(); // does not use m_id
};

template<>
struct foo_int_base< true >
{
  // stuff with the int
  int m_id = 0;
  void f(); // uses m_id
};

template< typename T, bool with_int = false >
struct foo : foo_int_base< with_int >
{
  // common stuff here
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM