简体   繁体   English

sizeof(…)= 0或C ++模板中的条件变量声明

[英]sizeof(…) = 0 or conditional variable declaration in c++ templates

Suppose I have something like this: 假设我有这样的事情:

struct EmptyClass{};
template<typename T1, typename T2 = EmptyClass,
         typename T3 = EmptyClass, typename T4 = EmptyClass,
         ..., typename T20> class PoorMansTuple {
  T1 t1;
  T2 t2;
  ...
  T20 t20;
};

Now, I may waste up to 19bytes per PoorMansTuple. 现在,每个PoorMansTuple最多可能浪费19个字节。

Question is: 问题是:

1) Is there a way to create a class of size 0? 1)有没有办法创建大小为0的类?

2) Is there a way to conditionally define a varaible? 2)有没有办法有条件地定义一个变量? Somethign like: 像这样的东西:

  T1 t1;
  if (T2 != EmptyClass) T2 t2; // pseudo code
  if (T3 != EmptyClass) T3 t3; // ...

Thanks! 谢谢!

The use of black magic macros is premitted. 允许使用黑色魔术宏。

I'm using g++ on MacOSX. 我在MacOSX上使用g ++。

Partial specialization may be what you are looking for the first part of the question. 部分专业化可能是您正在寻找的问题的第一部分。 This program 这个程序

#include <string>
#include <iostream>

struct EmptyClass {};

template<typename T1, typename T2>
class Tuple
{
   T1 t1;
   T2 t2;
};

template<typename T1>
class Tuple <T1, EmptyClass>
{
   T1 t1;
};


int main (void)
{
    Tuple<std::string, std::string> two;
    Tuple<std::string, EmptyClass> one1;
    Tuple<std::string> one2;

    std::cout << "<string, string>: " << sizeof(two) << std::endl;
    std::cout << "<string, empty> : " << sizeof(one1) << std::endl;
    std::cout << "<string>        : " << sizeof(one2) << std::endl;

    return 0;
}

prints 版画

<string, string>: 32
<string, empty> : 16
<string>        : 16

1) No, because the instance of the class couldn't have a memory adress. 1)不,因为该类的实例不能具有内存地址。 It requires at least 1 byte to have an adress. 地址至少需要1个字节。 -- that said, a class that don't have any instance and direct reference (used only at template generation for example) will have no size as it not be in the compiled program. -就是说,没有任何实例和直接引用的类(例如,仅用于生成模板的类)将没有大小,因为它不在编译程序中。

2) Not without macro...or maybe obscure template black arts that only boost.org ninjas can master. 2)并非没有宏...或者只有boost.org忍者才能掌握的晦涩的模板妖术。 I've heard of the idea of compile-time "if" but it's not currently in any coming standard of the language AFAIK. 我听说过编译时“ if”的想法,但是目前在AFAIK语言的任何新标准中都没有。 That would have allowed it. 那会允许的。 As already said, maybe there is a trick to make it. 如前所述,也许有一个技巧。

  1. A class has to have some size (at least one). 一堂课必须有一定规模(至少一个)。 See " What Is the Smallest Object Size Possible in C/C++? " 请参阅“ C / C ++中可能的最小对象大小是多少?
  2. AFAIK, no you can't. AFAIK,不,您不能。

Check out boost::tuple and boost::compressed_pair . 查看boost::tupleboost::compressed_pair A class can't have a sizeof 0, but there is the concept of the "empty base class" optimization. 一个类的大小不能为0,但是有一个“空基类”优化的概念。 Eh, I'm just going to link to one of my previous answers, it is pretty relevant here IMO: What is std::pair? 嗯,我只想链接到我以前的答案之一,就在这里与IMO相关: 什么是std :: pair?

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

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