今天我只想提出一个关于C++模板函数参数推导和模板函数重载决议的问题在c ++ 11中(我使用的是vs2010 sp1).我已经定义了两个模板函数,如下所示:
功能#1:
template <class T>
void func(const T& arg)
{
cout << "void func(const T&)" <<endl;
}
Run Code Online (Sandbox Code Playgroud)
功能#2:
template <class T>
void func(T&& arg)
{
cout << "void func(T&&)" <<endl;
}
Run Code Online (Sandbox Code Playgroud)
现在考虑以下代码:
int main() {
//I understand these first two examples:
//function #2 is selected, with T deduced as int&
//If I comment out function #2, function#1 is selected with
//T deduced as int
{int a = 0; func(a);}
//function #1 is selected, with T is deduced …Run Code Online (Sandbox Code Playgroud)