Questions tagged [perfect-forwarding]

Perfect forwarding describes a property of C++11 function templates which allows correctly deducing arguments as lvalues or rvalues and forwarding them in the same form to other functions.

Perfect forwarding was not possible in C++03 because template argument deduction cannot distinguish rvalues from const lvalues. See N1385: The forwarding problem for more details of the problem in C++03.

Support for rvalue-references and reference collapsing in C++11 enable perfect forwarding, so that generic call wrappers can accept any argument type and forward those arguments to another callable object, preserving whether the original arguments were lvalues or rvalues. I.e. perfect forwarding preserves the original argument's value category.

Perfect forwarding is done by taking arguments by "universal reference" (a function parameter of type T&& where T is a template parameter) and forwarding them using std::forward<T>.

Use this tag for questions about writing C++11 functions to take any type of expression and questions about forwarding those arguments to other functions. Use or for more general questions.

527 questions
4 answers

What does T&& (double ampersand) mean in C++11?

I've been looking into some of the new features of C++11 and one I've noticed is the double ampersand in declaring variables, like T&& var. For a start, what is this beast called? I wish Google would allow us to search for punctuation like…
  • 854,327
  • 234
  • 1,573
  • 1,953
7 answers

What are the main purposes of std::forward and which problems does it solve?

In perfect forwarding, std::forward is used to convert the named rvalue references t1 and t2 to unnamed rvalue references. What is the purpose of doing that? How would that affect the called function inner if we leave t1 & t2 as lvalues? template…
  • 5,681
  • 4
  • 17
  • 7
6 answers

make_unique and perfect forwarding

Why is there no std::make_unique function template in the standard C++11 library? I find std::unique_ptr p(new SomeUserDefinedType(1, 2, 3)); a bit verbose. Wouldn't the following be much nicer? auto p =…
  • 256,549
  • 94
  • 388
  • 662
4 answers

What's the difference between std::move and std::forward

I saw this here: Move Constructor calling base-class Move Constructor Could someone explain: the difference between std::move and std::forward, preferably with some code examples? How to think about it easily, and when to use which
  • 6,935
  • 14
  • 53
  • 89
1 answer

How would one call std::forward on all arguments in a variadic function?

I was just writing a generic object factory and using the boost preprocessor meta-library to make a variadic template (using 2010 and it doesn't support them). My function uses rval references and std::forward to do perfect forwarding and it got me…
5 answers

Capturing perfectly-forwarded variable in lambda

template void doSomething(T&& mStuff) { auto lambda([&mStuff]{ doStuff(std::forward(mStuff)); }); lambda(); } Is it correct to capture the perfectly-forwarded mStuff variable with the &mStuff syntax? Or is there a specific…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
2 answers

Is there a difference between universal references and forwarding references?

An argument to this function will bind to an rvalue reference: void f(int && i); However, an argument to this function will bind to either an rvalue or an lvalue reference: template void f(T && t); I've often heard this referred…
2 answers

Perfect forwarding - what's it all about?

Possible Duplicate: Advantages of using forward Could someone please explain to me what perfect forwarding is about?
  • 16,662
  • 33
  • 107
  • 151
1 answer

Should all/most setter functions in C++11 be written as function templates accepting universal references?

Consider a class X with N member variables, each of some copiable and movable type, and N corresponding setter functions. In C++98, the definition of X would likely look something like this: class X { public: void set_a(A const& a) { _a = a; } …
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
2 answers

Can I typically/always use std::forward instead of std::move?

I've been watching Scott Meyers' talk on Universal References from the C++ and Beyond 2012 conference, and everything makes sense so far. However, an audience member asks a question at around 50 minutes in that I was also wondering about. Meyers…
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1 answer

How to perfectly forward `auto&&` in a generic lambda?

C++14 supports generic lambdas. However, the following code is rejected by clang 3.4. #include void f(int); void f(int&); int main() { [](auto&& v) { f(std::forward(v)); }(8); // error } How to perfectly forward auto&& in a…
  • 39,765
  • 26
  • 162
  • 323
3 answers

Forwarding of return values. Is std::forward is needed?

I am writing library which wraps a lot of functions and methods from other library. To avoid coping of return values I am applying std::forward like so: template T&& wrapper(T&& t) { f(t); // t passed as lvalue return…
Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53
2 answers

Why use a perfectly forwarded value (a functor)?

C++11 (and C++14) introduces additional language constructs and improvements that target generic programming. These include features such as; R-value references Reference collapsing Perfect forwarding Move semantics, variadic templates and more I…
  • 30,036
  • 10
  • 99
  • 142
5 answers

How useful would Inheriting Constructors be in C++?

As I sit in the C++ Standards committee meetings, they are discussing the pros and cons of dropping Inheriting Constructors since no compiler vendor has implemented it yet (the sense being users haven't been asking for it). Let me quickly remind…
Faisal Vali
  • 32,723
  • 8
  • 42
  • 45
1 answer

What is the best way of renaming (alias/forward) a function in C++?

(I'll restrict this question to C++11, since I believe there is no general way to do this in C++98). Supposed I have a complicated (in terms of signature) set of template functions and/or overloaded functions, and I want to use these functions in…
  • 14,261
  • 4
  • 67
  • 118
2 3
35 36