std::forward Visual Studio 13 的行为不像我预期的那样
std::forward Visual Studio 13 doesn't behave like I expect
我正在尝试学习一些基本的C++11,使用Scott Meyers在youtube上的讲座"一个有效的C++11/14采样器"
https://www.youtube.com/watch?v=BezbcQIuCsY
使用他的std::forward
示例代码(讲座中的第19分钟),我编写了以下代码来理解std::forward
的效果
#include "stdafx.h"
#include <string>
#include <utility>
class A
{
public:
void Foo(std::string&& s)
{
std::string s2 = std::forward<std::string>(s);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
std::string s3 = "Hello World";
a.Foo(s3);
a.Foo("Hello World");
return 0;
}
令人惊讶的是,它没有编译,a.Foo(s3)
不能隐式地从左值转换为右值。所以我把a.Foo(s3);
改成了a.Foo(std::move(s3));
,现在它可以编译了。然而,在对Foo的两次调用中,std::forward<std::string>(s);
都被解析为右值,并发生了移动操作(由于其缓冲区被窃取,s被重置为""
)。
所以我真的不明白std::forward
有什么好处,什么时候适用。我在这里错过了什么?
在不涉及模板参数推导/引用折叠的情况下调用std::forward<>
是没有意义的。
转发引用(Scott Meyers曾称之为"通用引用")的意义在于,根据您收到的内容的价值类别,您也可以转发该价值类别。
但在这里,你一点也不困惑什么是价值类别,它是静态的。
以下是一个具有模板参数推导的上下文:
template<typename T>
void f(T&& t) // T is to be deduced, && might be collapsed
{
g(std::forward<T>(t)); // will keep the category value
}
f(std::string{"hey"}); // T inferred std::string&&, so the parameter type is `std::string&& &&`, which is collapsed to `std::string &&`.
您需要一个转发引用:
#include <string>
#include <utility>
class A
{
public:
template <typename String>
void Foo(String&& s)
{
std::string s2 = std::forward<String>(s);
}
};
int main()
{
A a;
std::string s3 = "Hello World";
a.Foo(s3);
a.Foo("Hello World");
return 0;
}
实例
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 试图在visual studio上用C++创建一个桌面应用程序
- 如何在MS Visual Studio 2019中运行QT UI
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- Visual Studio Code - C++ Debugger 無法啟動
- Visual C++ constexpr Hints
- 为什么它不适用于Visual 2019的原因
- Visual Studio 2019:插入多个C++风格的单行注释
- Visual Studio Code "undefined reference to `WinMain@16'"
- visual在c++中将十进制数转换为二进制数
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- Visual Studio中的函数声明和函数定义问题
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- Visual C++(VS2017)中用户定义的转换不明确
- Visual C++GC接口如何启用它以及要包含哪个库
- visual是否可以在c++中创建一个接收无限数量相同类型(或至少相当数量)参数的函数
- visual c++,如何获取解决方案目录中的代码
- C++模块(MSVC)中的visual Forward声明
- std::forward Visual Studio 13 的行为不像我预期的那样