有关GCC4.7.0编译器对C++11(0x)标准的支持的讨论 编译器C++GCCC++0xC++11
程序员文章站
2024-03-06 18:53:38
...
近日想尝试一下C++的最新标准,于是在互联网上搜索了相关资料。
其中在开源中国社区看到了一段测试各个编译器对C++11标准支持程度的代码,链接如下:
http://my.oschina.net/u/186539/blog/58074
其中提到:
本人在实际测试时,发现该段代码不能通过编译(GCC4.6和GCC4.7,即使添加-std=c++0x),
需做出以下修改:
*第27行 is_r_value(std::move<int>(i));
改为 is_r_value(std::move(i));
原因:move()函数用不到模板。
*第43行 constexpr int GetFive() {return 5;}
改为:移动到main()函数之外;
原因:函数之内不能再定义函数。
*第47行 decltype(v[0])b;
改为 decltype(v[0])b=0;
原因:编译器要求初始化b,因为decltype(v[0])推断出的类型是const int &,是个引用类型;
以下摘自*,http://en.wikipedia.org/wiki/C%2B%2B11
BTW:读者有没有发现一些诡异的相似之处呢?
欢迎指正。
其中在开源中国社区看到了一段测试各个编译器对C++11标准支持程度的代码,链接如下:
http://my.oschina.net/u/186539/blog/58074
其中提到:
引用
//=====测试结果
测试标准:编译对测试代码不报错为支持,否则不支持
//1.Visual Studio 2010 SP1(中文旗舰版) 全部不支持
//2.MinGW 20120426 GNU GCC 4.6.2 支持前两个,不支持后两个
//(注意,编译时应该开启新特性 -std=c++0x 或者 -std=gnu++0x)
//命令: g++ TestCpp11_1.cpp -o TestCpp11_1 -std=c++0x
//3.MinGW Distro 9.0 GNU GCC 4.7.0
// 很高兴,全部支持
// (注意,编译时应该开启新特性 -std=c++0x 或者 -std=gnu++0x)
// 命令: g++ TestCpp11_1.cpp -o TestCpp11_1 -std=c++0x
//4.Visual C++ 6.0 SP6(中文企业版) 全部不支持
测试标准:编译对测试代码不报错为支持,否则不支持
//1.Visual Studio 2010 SP1(中文旗舰版) 全部不支持
//2.MinGW 20120426 GNU GCC 4.6.2 支持前两个,不支持后两个
//(注意,编译时应该开启新特性 -std=c++0x 或者 -std=gnu++0x)
//命令: g++ TestCpp11_1.cpp -o TestCpp11_1 -std=c++0x
//3.MinGW Distro 9.0 GNU GCC 4.7.0
// 很高兴,全部支持
// (注意,编译时应该开启新特性 -std=c++0x 或者 -std=gnu++0x)
// 命令: g++ TestCpp11_1.cpp -o TestCpp11_1 -std=c++0x
//4.Visual C++ 6.0 SP6(中文企业版) 全部不支持
本人在实际测试时,发现该段代码不能通过编译(GCC4.6和GCC4.7,即使添加-std=c++0x),
需做出以下修改:
*第27行 is_r_value(std::move<int>(i));
改为 is_r_value(std::move(i));
原因:move()函数用不到模板。
*第43行 constexpr int GetFive() {return 5;}
改为:移动到main()函数之外;
原因:函数之内不能再定义函数。
*第47行 decltype(v[0])b;
改为 decltype(v[0])b=0;
原因:编译器要求初始化b,因为decltype(v[0])推断出的类型是const int &,是个引用类型;
以下摘自*,http://en.wikipedia.org/wiki/C%2B%2B11
引用
The type denoted by decltype can be different from the type deduced by auto.
#include <vector>
int main()
{
const std::vector<int> v(1);
auto a = v[0]; // a has type int
decltype(v[0]) b = 1; // b has type const int&, the return type of
// std::vector<int>::operator[](size_type) const
auto c = 0; // c has type int
auto d = c; // d has type int
decltype(c) e; // e has type int, the type of the entity named by c
decltype((c)) f = c; // f has type int&, because (c) is an lvalue
decltype(0) g; // g has type int, because 0 is an rvalue
}
#include <vector>
int main()
{
const std::vector<int> v(1);
auto a = v[0]; // a has type int
decltype(v[0]) b = 1; // b has type const int&, the return type of
// std::vector<int>::operator[](size_type) const
auto c = 0; // c has type int
auto d = c; // d has type int
decltype(c) e; // e has type int, the type of the entity named by c
decltype((c)) f = c; // f has type int&, because (c) is an lvalue
decltype(0) g; // g has type int, because 0 is an rvalue
}
BTW:读者有没有发现一些诡异的相似之处呢?
欢迎指正。