小编cha*_*eng的帖子

c ++模板函数参数推导和函数解析

今天我只想提出一个关于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)

c++ templates resolution function c++11

4
推荐指数
1
解决办法
413
查看次数

标签 统计

c++ ×1

c++11 ×1

function ×1

resolution ×1

templates ×1