C++ Perfect Forwarding

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


Perfect forwarding requires forwarding references in order to preserve the ref-qualifiers of the arguments. Such references appear only in a deduced context. That is:

template<class T>
void f(T&& x) // x is a forwarding reference, because T is deduced from a call to f()
    g(std::forward<T>(x)); // g() will receive an lvalue or an rvalue, depending on x

The following does not involve perfect forwarding, because T is not deduced from the constructor call:

template<class T>
struct a
    a(T&& x); // x is a rvalue reference, not a forwarding reference

C++17 will allow deduction of class template arguments. The constructor of "a" in the above example will become a user of a forwarding reference

a example1(1);
  // same as a<int> example1(1);

int x = 1;
a example2(x);
  // same as a<int&> example2(x);

Got any C++ Question?