VS2013中的C++11新特性

介绍

Visual C++ 2013 Preview 在6月发布了,C++开发者又找到一个编译器可以更好的支持ISO C++ 11 的特性了.本文介绍了这些新的特性并附有代码实例.

你想动手尝试编译文中的这些代码话,需要去下载并安装Visual Studio 2013 Preview (话说:付费吗?),我尚未在其他编译器上测试这些代码,所以我并不知道与Gcc 或Clang的兼容性(可恶的C++).


原始字符串字面值

VC++ 2013现在支持原始字符串字面值了。注意:它并不支持统一码字符串字面值。一个原始字符串字面值允许你避免转义那些在HTML,XML和正则表达式里运用得得心应手的特殊字符。下面是一个示例用法:

1auto s1 = R"(This is a "raw" string)";
现在,s1是一个指向常量字符串值为“This is a "raw" string”的指针。尽管不支持嵌套双引号,这与C#支持的@string文字是类似的。那么要在一个字符串字面值中嵌入R"(...)"会怎样。这种情况下,你可以使用以下语法:
1auto s2 = R"QQ(Example: R"(This is my raw string)")QQ";
现在,s2包含 Example: R"(This is my raw string)"。 在这个例子中,我把QQ作为界定符。这个界定符可以是任何长度不超过16的字符串。原始字符串字面值也可以包含换行:
1auto s3 = R"(<tr>
2<td>data</td>
3</tr>)";
最后,不论他们什么时候添加统一码字符串字面值的支持,你都可以将它们连接起来并构成原始统一码字符串字面值。


可变参数模板

可变参数模板是一个允许多个参数的模板。在我看来,这是个提供给库作者而不是给库使用者的特性,所以我也不是很确定它在C++程序员中会有多流行。以下我们用一个非常简单的例子来展示如何在实际开发中使用可变参数模板。

01// Variadic template declaration
02template<typename... Args> class Test;
03 
04// Specialization 1
05template<typename T> class Test<T>
06{
07public:
08  T Data;
09};
10 
11// Specialization 2
12template<typename T1, typename T2> class Test<T1, T2>
13{
14public:
15  T1 Left;
16  T2 Right;
17};
18 
19void Foo()
20{
21  Test<int> data;
22  data.Data = 24;
23 
24  Test<int, int> twovalues;
25  twovalues.Left = 12;
26  twovalues.Right = 15;
27}

当使用可变参数模板时,智能感应(intellisense)能很好地配合我们的开发。可变参数模板的实现包括一个叫asizeof的函数,这个函数能返回这个模板的参数个数。

01template<typename... Args> class Test
02{
03public:
04  size_t GetTCount()
05  {
06    return sizeof...(Args);
07  }
08};
09 
10// . . .
11 
12Test<int> data;
13size_t args = data.GetTCount(); //1
14 
15Test<int, int, char*> data2;
16args = data2.GetTCount(); //3
17 
18Test<int, float> data3;
19args = data3.GetTCount(); //2

这其实就是一个数个数的例子,但我猜他们之所以使用一个现存的函数名是因为这样子做会让C++程序员们更容易上手。

对于可变参数模板,一个常用的做法就是专攻其中一个参数,然后把其余的参数都变为可选。这个做法可以以递归的形式实现。以下是一个比较傻的例子,但它能让你明白什么时候不应该用可变参数模板,继而更好地了解这个语言特性。

01template<typename... Args> class Test;
02 
03// Specialization for 0 arguments
04template<> class Test<>
05{
06};
07 
08// Specialization for at least 1 argument
09 
10template<typename T1, typename... TRest> class Test<T1, TRest...>
11  : public Test<TRest...>
12{
13public:
14  T1 Data;
15 
16  // This will return the base type
17  Test<TRest...>& Rest()
18  {
19    return *this;
20  }
21};
22 
23void Foo()
24{
25  Test<int> data;
26  data.Data = 24;
27 
28  Test<int, int> twovalues;
29  twovalues.Data = 10;
30  // Rest() returns Test<int>
31  twovalues.Rest().Data = 11;
32 
33  Test<int, int, char*> threevalues;
34  threevalues.Data = 1;
35  // Rest() returns Test<int, int>
36  threevalues.Rest().Data = 2;
37  // Rest().Rest() returns Test<char*>
38  threevalues.Rest().Rest().Data = "test data";
39}

大家请注意了,千万别把代码写成这样。这个例子仅仅是用来教学的,正确的做法我会在下一个章节中告诉大家。

Tuple的实现

我们来看一下std tuple的头文件 (由VC++团队的Stephan T. Lavavej负责维护 - 最初的代码由P.J. Plauger编写),浏览这些代码,让我的大脑几乎要宕掉了。为了更好的理解代码,我将代码进行简化,摘出其中可以访问tuple的值的最少的代码(能够支持读和写)。这有助于理解在设计模板类时,通常可变参数模板是如何通过递归展开来大幅减少代码的行数。

01// tuple
02template<class... _Types> class tuple;
03 
04// 空tuple
05template<> class tuple<> {};
06 
07// 递归的tuple定义
08template<class _This,
09  class... _Rest>
10  class tuple<_This, _Rest...>
11  : private tuple<_Rest...>
12{
13public:
14  _This _Myfirst;
15};

这里的递归特化使用了继承,因此tuple的每个类型成员都确定的时候递归会终止。读取tuple值的时候,tuple_element类起到读取辅助类的作用。

01// tuple_element
02template<size_t _Index, class _Tuple> struct tuple_element;
03 
04// select first element
05template<class _This, class... _Rest>
06struct tuple_element<0, tuple<_This, _Rest...>>
07{
08  typedef _This& type;
09  typedef tuple<_This, _Rest...> _Ttype;
10};
11 
12// recursive tuple_element definition
13template <size_t _Index, class _This, class... _Rest>
14struct tuple_element<_Index, tuple<_This, _Rest...>>
15  : public tuple_element<_Index - 1, tuple<_Rest...> >
16{
17};
这里又一次使用了递归继承,同时边界条件也特化了。注意这里的两个typedef,其中一个定义为对应值类型的引用,另一个定义为和tuple_element类型参数相同的tuple类型。因此,给定一个_Index值,在那个递归层次上我们就能得到对应tuple的类型和tuple值的类型。下面的get方法就使用了这个特性。
1// get reference to _Index element of tuple
2template<size_t _Index, class... _Types> inline
3  typename tuple_element<_Index, tuple<_Types...>>::type
4  get(tuple<_Types...>& _Tuple)
5{
6  typedef typename tuple_element<_Index, tuple<_Types...>>::_Ttype _Ttype;
7  return (((_Ttype&) _Tuple)._Myfirst);
8}

注意返回类型,它使用上面定义的类型 typedef。同样,元组(tupleis)转换为上述定义过的类型 _TType ,然后我们访问 _Myfirst 成员 (它表示的值)。现在你可以如下所示,编写代码,

1tuple<int, char> t1;
2get<0>(t1) = 959;
3get<1>(t1) = 'A';
4 
5auto v1 = get<0>(t1);
6auto v2 = get<1>(t1);
现在 , 这 不用 说 , 但 我会 说 只是 可以 肯定 的是 ------ 这 里只 是 为了 演示 。 不 要在 实际 代码 中 使用 这些 方法, 而是调用 std::tuple, 它可以完成比 这 一切多的功能 ( 这就是为什么他有800行长).

代理构造函数

代理构造函数已经在C#中用了好长时间,所以将其引入到C++中也很不错。编译器允许一个类型的构造函数(代理构造函数)在其初始化列表中包含另一个构造函数。以前编写代码形式如下:

01class Error
02{
03public:
04  Error()
05  {
06    Init(0, "Success");
07  }
08 
09  Error(const char* message)
10  {
11    Init(-1, message);
12  }
13 
14  Error(int errorCode, const char* message)
15  {
16    Init(errorCode, message);
17  }
18 
19private:
20  void Init(int errorCode, const char* message)
21  {
22    //...
23  }
24};
采用代理构造函数是,可以写成如下形式:
01class Error
02{
03public:
04  Error() : Error(0, "Success")
05  {
06  }
07 
08  Error(const char* message) : Error(-1, message)
09  {
10  }
11 
12  Error(int errorCode, const char* message)
13  {
14    // ...
15  }
16};


函数模板中的默认模板参数

这是VC++ 2013现在支持的另一项C++ 11特性。目前为止,下面的代码仍然无法通过VC++编译。

1template <typename T = int> void Foo(T t = 0) { }
2 
3// error C4519: default template arguments are only
4// allowed on a class template
Visual C++ 2013 能够顺利编译这些代码,模板参数推断也能正确进行。
1Foo(12L); // Foo<long>
2Foo(12.1); // Foo<double>
3Foo('A'); // Foo<char>
4Foo(); // Foo<int>
这项特性的实用性在下面的例子里尤为明显。
01template <typename T> class Manager
02{
03public:
04  void Process(T t) { }
05};
06 
07template <typename T> class AltManager
08{
09public:
10  void Process(T t) { }
11};
12 
13template <typename T, typename M = Manager<T>> void Manage(T t)
14{
15  M m;
16  m.Process(t);
17}
18 
19Manage(25); // Manage<int, Manager<int>>
20Manage<int, AltManager<int>>(25); // Manage<int, AltManager<int>>
并不是所有的参数都需要默认参数。
1template <typename B, typename T = int> void Bar(B b = 0, T t = 0) { }
2 
3Bar(10); // Bar<int, int>
4Bar(10L); // Bar<long, int>
5Bar(10L, 20L); // Bar<long, long>
6Bar(); // will not compile
如果带默认参数的函数模板有重载的话,类型无法推断的时候编译器将会给出错误。
1template <typename T = int> void Foo(T t = 0) { }
2template <typename B, typename T = int> void Foo(B b = 0, T t = 0) { }
3 
4Foo(12L); // will not compile
5Foo(12.1); // will not compile
6Foo('A'); // will not compile
7Foo(); // Foo<int>

使用函数模板的默认模板参数时应当在这里注意。

显式转换运算符

我仍然记得2004年八月的一天,那个时候我意识到尽管我是一个还不错的C++程序员,我对explicit关键字一无所知,这令我十分局促不安。那之后我写了一篇博客文章

简单说明一下explicit的使用。考虑一下下面的例子。

01class Test1
02{
03public:
04  explicit Test1(int) { }
05};
06 
07void Foo()
08{
09  Test1 t1(20);
10  Test1 t2 = 20; // will not compile
11}
尽管转换构造函数可以达到这一目的,转换运算符因为缺乏标准支持而无法完成类似的任务。坏消息是你无法确保转换构造函数和转换运算符的行为是一致的。考虑一下下面的例子。
01class Test1
02{
03public:
04  explicit Test1(int) { }
05};
06 
07class Test2
08{
09  int x;
10public:
11  Test2(int i) : x(i) { }
12  operator Test1() { return Test1(x); }
13};
14 
15void Foo()
16{
17  Test2 t1 = 20;
18  Test1 t2 = t1; // will compile
19}
上面的代码能通过编译。现在有了C++ 11的新特性,explicit也可以用在转换运算符上了。
01class Test2
02{
03  int x;
04public:
05  Test2(int i) : x(i) { }
06  explicit operator Test1() { return Test1(x); }
07};
08 
09void Foo()
10{
11  Test2 t1 = 20;
12  Test1 t2 = (Test1)t1; // this compiles
13  Test1 t3 = t1; // will not compile
14}
下面的这个例子里隐式应用了bool类型的转换运算符。
01class Test3
02{
03public:
04  operator bool() { return true; }
05};
06 
07void Foo()
08{
09  Test3 t3;
10  if (t3)
11  {
12  }
13 
14  bool b = t3;
15}
这段代码能通过编译。现在试一下在运算符上加上explicit关键字
01class Test3
02{
03public:
04  explicit operator bool() { return true; }
05};
06 
07void Foo()
08{
09  Test3 t3;
10  if (t3) // this compiles!
11  {
12  }
13 
14  bool b = t3; // will not compile
15}

正如预期,第二个转换无法通过编译,但是第一个通过了。这是因为if里的bool转换被视为显式转换。因此在这里你要小心谨慎,仅仅添加explicit关键字无法防止意外情况下的类型转换,类型可能仍然是不安全的。

初始化列表和统一初始化

一直以来我们都可以用初始化列表初始化数组,现在对于有类型为std::initializer_list<T>(包含构造函数)的类型我们也可以这么做。标准库中的容器现在都支持这一特性。

01void foo()
02{
03  vector<int> vecint = { 3, 5, 19, 2 };
04  map<int, double> mapintdoub =
05  {
06    { 4, 2.3},
07    { 12, 4.1 },
08    { 6, 0.7 }
09  };
10}
自己实现这些功能很浪费时间

1void bar1(const initializer_list<int>& nums)
2{
3  for (auto i : nums)
4  {
5    // use i
6  }
7}
8 
9bar1({ 1, 4, 6 });
用户自定义类型也可以使用这一特性

01class bar2
02{
03public:
04  bar2(initializer_list<int> nums) { }
05};
06 
07class bar3
08{
09public:
10  bar3(initializer_list<bar2> items) { }
11};
12 
13bar2 b2 = { 3, 7, 88 };
14 
15bar3 b3 = { {1, 2}, { 14 }, { 11, 8 } };
C++11也新增了一个相关特性:统一初始化( Uniform initialization)。这一特性将自动匹配合适的构造函数

01class bar4
02{
03  int x;
04  double y;
05  string z;
06 
07public:
08  bar4(int, double, string) { }
09};
10 
11class bar5
12{
13public:
14  bar5(int, bar4) { }
15};
16 
17bar4 b4 { 12, 14.3, "apples" };
18 
19bar5 b5 { 10, { 1, 2.1, "bananas" } };
使用初始化列表的构造函数将被优先使用

01class bar6
02{
03public:
04  bar6(int, int) // (1)
05  {
06    // ...
07  }
08 
09  bar6(initializer_list<int>) // (2)
10  {
11    // ...
12  }
13};
14   
15bar6 b6 { 10, 10 }; // --> calls (2) above
就是这些内容。像往常一样,欢迎反馈和批评建议。谢谢。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值