c++11 统一初始化不适用于 "g++ -std=c++0x"

c++11 uniform initialization doesn't work with "g++ -std=c++0x"

我有一个 class 声明了这个 public 方法:

virtual std::vector<float> operator()(const std::vector<float>& = {});

使用统一初始化(这里只是 {}),这是 c++11 的一个特性。使用 clang++ -std=c++11 编译时,这不会给我带来任何问题。但是当我使用 g++ -std=c++0x 我得到这个:

error: expected primary-expression before '{' token

-std=c++0x 选项不是应该给我带来 c++11 支持吗?

使用标准 C++ 声明方法时,编译器没有给我任何错误:

virtual std::vector<float> operator()(const std::vector<float>& = std::vector<float>());

我在 Ubuntu 12.04

上使用 g++ 4.6

来自GCC 4.7 release notes

G++ now accepts the -std=c++11, -std=gnu++11, and -Wc++11-compat options, which are equivalent to -std=c++0x, -std=gnu++0x, and -Wc++0x-compat, respectively.

来自C++11 in GCC project page

GCC 4.8.1 was the first feature-complete implementation of the 2011 C++ standard, previously known as C++0x.


坏消息,您需要升级编译器才能获得对 C++11 的支持。

GCC 4.6 不支持所有的 c++11 特性:

GCC provides experimental support for the upcoming ISO C++ standard, C++0x. This support can be enabled with the -std=c++0x.

我建议您升级到最新的 GCC 版本,并使用标志 -std=c++11 甚至 -std=c++14

进行编译