小编Ank*_*lla的帖子

如何在不使用模板的情况下将左值传递给仅采用右值的函数

我的问题很简单

#include <iostream>

using namespace std;

template <typename T>
void f(T&& i) {
        cout << i << endl;
}

void g(int&& i) {
        cout << i << endl;
}

int main() {
        int i = 0;
        f(i); // works fine
        g(i); // candidate function not viable: no known conversion from 'int' to 'int &&'
              // for 1st argument void g(int&& i)

}
Run Code Online (Sandbox Code Playgroud)

为什么我可以将左值传递给模板化函数f()而不是非模板化函数g()

c++ templates rvalue lvalue

3
推荐指数
1
解决办法
71
查看次数

标签 统计

c++ ×1

lvalue ×1

rvalue ×1

templates ×1