相关疑难解决方法(0)

为什么std :: array <T,0>不为空?

鉴于此std::array< T, 0 >,为什么它不是空的?我的意思是"空",如:

 std::is_empty< std::array< int, 0 > >::value
Run Code Online (Sandbox Code Playgroud)

回来false

 #include <iostream>
 #include <tuple>
 #include <array>

 struct Empty {};

 int main()
 {
     std::cout << sizeof(std::tuple<int>) << std::endl;
     std::cout << sizeof(std::tuple<int,Empty>) << std::endl;
     std::cout << sizeof(std::tuple<int,std::array<int,0>>) << std::endl;
 }
Run Code Online (Sandbox Code Playgroud)

产量

 4
 4
 8
Run Code Online (Sandbox Code Playgroud)

这意味着,std::array<int,0>没有应用空基础优化(EBO).

这对我来说似乎特别奇怪std::tuple<>(注意:没有模板参数)空的,即std::is_empty<std::tuple<>>::value产量true.

问题:为什么这样,因为这个尺寸0已经是一个特例了std::array?这是标准的故意还是疏忽?

c++ language-lawyer c++11

21
推荐指数
1
解决办法
1459
查看次数

C++ 11:v = {}和v {}之间的区别

我想问一下以下两个陈述之间是否有任何区别:

// C++11
std::vector<int> d {1, 2, 3};
std::vector<int> d = {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)

在这两种情况下都会调用序列构造函数:

class A {
public:
  int a;

  A() {
    cout << "default constructor" << endl;
  };

  A(const A& other) {
    cout << "copy constructor" << endl;
  };

  A& operator =(const A& other) {
    cout << "assignment operator" << endl;
  }

  A(std::initializer_list<int> e) {
    cout << "sequence constructor" << endl;
  };

  A& operator =(std::initializer_list<int> e) {
    cout << "initializer list assignment operator" << endl;
  }
};

int …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

5
推荐指数
1
解决办法
390
查看次数

标签 统计

c++ ×2

c++11 ×2

language-lawyer ×1