您的位置:首页 > 编程语言 > C语言/C++

测试编译器是否支持C++11新特性(1)

2012-09-15 16:40 489 查看
http://my.oschina.net/u/186539/blog/58074

01
/*-==========================================================
02
*文件名:TestCpp11_1.cpp
03
*开发人员:袁培荣
04
*当前版本:1.0.0.2595
05
*创建时间:2012-05-20
06
*修改时间:2012-05-20
07
*功能说明:测试编译器是否支持C++11新特性(1)
08
*版权说明:版权所有袁培荣YuanPeirong

09
*编译环境:Windows7(x64)SP1简体中文专业版
10
*编译器:VisualStudio2010SP1(中文旗舰版)
11
MinGW20120426GNUGCC4.6.2
12
MinGWDistro9.0GNUGCC4.7.0
13
VisualC++6.0SP6(中文企业版)
14
-==========================================================*/
15
16
#include<iostream>
17
#include<vector>
18
using
namespace
std;
19
20
//=====1.测试右值引用和move语意
21
bool
is_r_value(
int

&&){
return
true
;}
22
bool
is_r_value(
const

int
&){
return

false
;}
23
24
void
test(
int

&&i)
25
{
26
is_r_value(i);
27
is_r_value(std::move<
int
>(i));
28
}
29
//====================================
30
31
int
main(
int

argc,
char
*argv[])
32
{
33
34
//=====2.测试以范围为基础的for循环
35
int

my_array[5]={1,2,3,4,5};
36
for

(
int

&x:my_array)
37
{
38
x*=2;
39
}
40
//====================================
41
42
//=====3.测试型别推导
43
constexpr
int
GetFive(){
return

5;}
44
int

some_value[GetFive()+5];
45
const

std::vector<
int
>v(1);
46
autoa=v[0];
47
decltype(v[0])b;
48
autoc=0;
49
autod=c;
50
decltype(c)e;
51
decltype((c))f=e;
52
decltype(0)g;
53
//====================================
54
return

0;
55
}
56
57
58
59
//=====4.测试初始化表达式
60
class
C
61
{
62
int

a=7;
//在类的定义时初始化非静态变量,只有C++11支持
63
public
:
64
C();
65
};
66
//====================================
67
68
69
//====================================
70
//=====测试结果
71
72
//测试标准:编译对测试代码不报错为支持,否则不支持
73
74
//1.VisualStudio2010SP1(中文旗舰版)全部不支持
75
76
//2.MinGW20120426GNUGCC4.6.2
77
//支持前两个,不支持后两个
78
//(注意,编译时应该开启新特性-std=c++0x或者-std=gnu++0x)
79
//命令:g++TestCpp11_1.cpp-oTestCpp11_1-std=c++0x
80
81
//3.MinGWDistro9.0GNUGCC4.7.0
82
//很高兴,全部支持
83
//(注意,编译时应该开启新特性-std=c++0x或者-std=gnu++0x)
84
//命令:g++TestCpp11_1.cpp-oTestCpp11_1-std=c++0x
85
86
//4.VisualC++6.0SP6(中文企业版)全部不支持
87
//====================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: