基于 http://www.cplusplus.com/reference/stl/vector/vector/
explicit vector ( const Allocator& = Allocator() );
Run Code Online (Sandbox Code Playgroud)
此向量构造函数采用具有默认值Allocator()的引用参数.我从这个函数签名中学到的是函数可以使用默认值的引用参数.
这是我和VS2010一起玩的演示代码.
#include "stdafx.h"
#include <iostream>
using namespace std;
void funA(const int& iValue=5) // reference to a template const int 5 why?
{
cout << iValue << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
funA();
funA(10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否有一些规则来指导这种语法用法(即带有默认值的引用参数)?
Pup*_*ppy 12
Const引用可以绑定到临时对象,在这种情况下,临时对象的生命周期延伸到引用的生命周期.