小编Mat*_*vei的帖子

为什么这段代码会为不同的编译器生成不同的输出?

我有以下代码:

#include <iostream>
#include <vector>

struct C {
  int a;

  C() : a(0) {}
  C(int a) : a(a) {}
};

std::ostream &operator<<(std::ostream &os, const C &c) {
  os << c.a;
  return os;
}

using T = std::vector<C>;

int main() {
  auto v = T({5});

  for (const auto &a : v) {
    std::cout << a << ", ";
  }
  std::cout << std::endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我使用 g++,它会打印:

#include <iostream>
#include <vector>

struct C {
  int a;

  C() : a(0) {}
  C(int …
Run Code Online (Sandbox Code Playgroud)

c++ vector initializer-list compiler-specific direct-initialization

20
推荐指数
1
解决办法
853
查看次数