我一直认为std::forward这只适用于模板参数.但是,我问自己为什么.请参阅以下示例:
void ImageView::setImage(const Image& image){
    _image = image;
}
void ImageView::setImage(Image&& image){
    _image = std::move(image);
}
这两个功能基本上是一样的; 一个采用l值参考,另一个采用r值参考.现在,我认为,std::forward如果参数是l值引用,则应返回l值引用,如果参数为1,则应返回r值引用,此代码可简化为如下所示:
void ImageView::setImage(Image&& image){
    _image = std::forward(image);
}
这类似于cplusplus.com提到的示例std::forward(只是没有任何模板参数).我想知道,如果这是正确与否,如果不是为什么.
我也问自己究竟会有什么区别
void ImageView::setImage(Image& image){
    _image = std::forward(image);
}
我在Effective Modern C++中看到的代码片段巧妙地实现了创建函数计时器的工具原理:
auto timeFuncInvocation = 
    [](auto&& func, auto&&... params)
    {
        start timer; 
        std::forward<decltype(func)>(func)(
            std::forward<decltype(params)>(params)...); 
        stop timer and record elapsed time; 
    };
我的问题是关于 std::forward<decltype(func)>(func)(...
对于在lambda表达式中使用熟悉的模板语法,这似乎是一个很好的用例,以防我们想要使计时器类型成为编译时常量.
我注意到std::forward在这方面没有用处:
void consumeObject(std::unique_ptr<Object>&& robj) {
  myvector.emplace_back(std::forward<std::unique_ptr<Object>>(robj));
}
这是为什么?我认为当rvalue引用绑定到一个函数参数时(即我可以通过名称引用它),它在该范围内变为左值,并且为了被传递,需要被转换为右值引用(如在完美转发中) ).
为什么它错了/没用?
我为什么要或不应该创建我的所有函数和成员函数来取一个带有a的rvalue版本lvalue?你总是可以转发lvalue到右转,对吧?我甚至可以拥有const rvalue,为什么这是一个坏主意或好主意呢?
我在代码中的意思如下.该"&&" rvalue引用允许用户使用临时对象,并且仍然可以lvalue通过简单的转发使用.所以考虑到这一点,我为什么要提供print_string(string& str)(lvalue参考)c ++ 11中的任何函数(除了const参考,因为rvalue有好的)?
#include <iostream>
#include <string>
#include <utility>
using namespace std;
void print_string(string&& str)
{
    str += "!!!";
    cout << str << endl;
}
void print_string2(string& str)
{
    str += "???";
    cout << str << endl;
}
int main(int argc, char* argv[])
{
    print_string("hi there");  // works
    print_string(string("hi again")); // works
    string lvalue("lvalue");
    print_string(forward<string>(lvalue)); // …在关于通用引用的讲座中,Scott Meyers(大约第40分钟)说,在使用之前,应该将通用引用的对象转换为实际类型.换句话说,只要存在具有通用引用类型的模板函数,就std::forward应该在使用运算符和表达式之前使用它,否则可能会生成对象的副本.
我对此的理解如下:
#include <iostream>
struct A
{
  A() { std::cout<<"constr"<<std::endl; }
  A(const A&) { std::cout<<"copy constr"<<std::endl; }
  A(A&&) { std::cout<<"move constr"<<std::endl; }
  A& operator=(const A&) { std::cout<<"copy assign"<<std::endl; return *this; }
  A& operator=(A&&) { std::cout<<"move assign"<<std::endl; return *this; }
  ~A() { std::cout<<"destr"<<std::endl; }
  void bar()
  {
    std::cout<<"bar"<<std::endl;
  }
};
A getA()
{
  A a;
  return a;
}
template< typename T >
void callBar( T && a )
{
  std::forward< T >( a ).bar(); …以下两个声明之间有什么区别?我应该何时优先选择另一个?
void f(unique_ptr<T> x);
void f(unique_ptr<T> &&x);
在我看来,这两个都意味着f()取得了指向对象的所有权,但我看到人们都使用它们.我希望第一个是标准的,第二个被认为是一个不好的做法.
std::exchange在cppreference.com上学习时,我遇到了下面粘贴的“可能的实现”。
  template<class T, class U = T>
  T exchange(T& obj, U&& new_value)
  {
      T old_value = std::move(obj);
      obj = std::forward<U>(new_value);
      return old_value;
  }
obj = std::forward在赋值语句中看到而不是看起来有点奇怪std::move,我认为这会产生相同的效果。
我的理解是,这std::move等效于static_cast右值引用类型(但更具表现力)并始终返回 xvalue,而std::forward返回传递的相同值类型。
我的问题是为什么std:forward在上面的代码片段中使用?是否有一个简单的经验法则来决定何时这是更好的选择?