我有一个声明这个公共(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
最佳答案
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.
GCC 4.8.1 was the first feature-complete implementation of the 2011 C++ standard, previously known as C++0x.
坏消息,您需要升级您的编译器才能获得有效的 C++11 支持。
关于c++11 统一初始化不适用于 "g++ -std=c++0x",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36920707/