使用 "itself" 初始化向量适用于 MSVC2013,不适用于 GCC 4.8.1

Initializing a vector with "itself" works with MSVC2013, not with GCC 4.8.1

本文关键字:适用于 GCC 不适用 MSVC2013 itself 初始化 向量 使用      更新时间:2023-10-16

使用GCC 4.8.1打印0(为什么不),而使用MSVC2013打印10(为什么?):

#include <iostream>
#include <vector>
const int y = 10;
int main()
{
  std::vector<double> y(y);
  std::cout << y.size() << std::endl;
  return 0;
}

我认为你需要与warning level 4 or warning is an error在visual studio和
-Wall and -Werror or -Winit-self or -Wuninitialized为ggcc.
编译,但是你有一个分配问题。

对我来说最好的标志是:-std=c++11 -pipe -m64 -ansi -fPIC -g -O3 -fno-exceptions -fstack-protector -Wl,-z,relro -Wl,-z,now -fvisibility=hidden -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -Werror