std::前进Visual Studio 13不';I don’我的行为不像我想象的那样 我正在努力学习一些基本的C++ 11,使用Scott Meyers在YouTube上的一个叫做“有效的C++ 11/14取样器”的讲座。
使用他的std::前进Visual Studio 13不';I don’我的行为不像我想象的那样 我正在努力学习一些基本的C++ 11,使用Scott Meyers在YouTube上的一个叫做“有效的C++ 11/14取样器”的讲座。,c++,c++11,visual-c++,visual-studio-2013,C++,C++11,Visual C++,Visual Studio 2013,使用他的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);
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;
}
#包括“stdafx.h”
#包括
#包括
甲级
{
公众:
void Foo(std::string&&s)
{
std::string s2=std::forward(s);
}
};
int _tmain(int argc,_TCHAR*argv[]
{
A A;
std::string s3=“你好世界”;
a、 Foo(s3);
a、 福(“你好世界”);
返回0;
}
令人惊讶的是,它没有编译,a.Foo(s3)
不能隐式地从左值转换为右值。所以我改变了a.Foo(s3)代码>到a.Foo(std::move(s3))代码>现在它可以编译了。
但是在对Foostd::forward的两个调用上代码>解析为右值,并发生移动操作(由于缓冲区被盗,s被重置为“”
)
所以我真的不明白什么是好的,什么时候适用。我错过了什么 您需要转发引用:
#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;
}
#包括
#包括
甲级
{
公众:
模板
void Foo(字符串(&s)
{
std::string s2=std::forward(s);
}
};
int main()
{
A A;
std::string s3=“你好世界”;
a、 Foo(s3);
a、 福(“你好世界”);
返回0;
}
您需要转发参考:
#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;
}
#包括
#包括
甲级
{
公众:
模板
void Foo(字符串(&s)
{
std::string s2=std::forward(s);
}
};
int main()
{
A A;
std::string s3=“你好世界”;
a、 Foo(s3);
a、 福(“你好世界”);
返回0;
}
在不涉及模板参数推断/引用折叠时调用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 &&`.
模板
void f(T&&T)//T将被推断,&&T可能被折叠
{
g(std::forward(t));//将保留类别值
}
f(std::string{“hey”});//T推断std::string&&,因此参数类型为'std::string&&&&&`,它被折叠为'std::string&&`。
当不涉及模板参数推断/引用折叠时调用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 &&`.
模板
void f(T&&T)//T将被推断,&&T可能被折叠
{
g(std::forward(t));//将保留类别值
}
f(std::string{“hey”});//T推断std::string&&,因此参数类型为'std::string&&&&&`,它被折叠为'std::string&&`。
std::string&
是右值引用,而不是转发引用(或一段时间以来的“通用引用”)。std::string&&
是右值引用,而不是转发引用(或一段时间以来的“通用引用”)。