c++ - 为什么此代码打印出 RValue 而不是 LValue
问题描述
我正在测试代码并陷入困境。
这是我的代码:
#include <iostream>
template<typename T>
void check(T&& other) {
std::cout << "Rvalue" << std::endl;
}
template<typename T>
void check(T& other) {
std::cout << "Lvalue" << std::endl;
}
template<typename T>
void call(T other) {
check(std::forward<T>(other));
}
int main() {
std::string t = "Cool";
call(t);
}
输出:
RValue
为什么这个“RValue”的输出?我确实传递了一个 LValue,当它转发时,它不是作为 LValue 转发的吗?为什么会调用check的RValue函数呢?
解决方案
推荐阅读
- html - 如何消除某些电子邮件客户端上的图像之间的间隙(当单元格间距/填充已等于 0 时)
- python - 尝试导入 PuLP 时出错 - ImportError: cannot import name 'clock' from 'time' (unknown location)
- angular - Angular Universal - 在 main.js 加载后渲染
- swift - 在 NSPredicate 中比较整数和字符串
- c# - Select2 字段未在局部视图内呈现
- php - 扩展一个 PHP 类及其子类
- pyspark - 在 Pyspark 的数据框中添加每组总数作为新行
- arrays - 将布尔数组作为函数输入传递时初始化器值过多
- python - 有没有办法在划分列表后只接受整数值?已回答
- python - 试图计算文本文件的每一行中有多少个数字