perfect-forwarding

Perfect forwarding a member of object

五迷三道 提交于 2019-11-28 07:36:59
Suppose I have two struct s: struct X {}; struct Y { X x; } I have functions: void f(X&); void f(X&&); How do I write a function g() that takes Y& or Y&& but perfect forwarding X& or X&& to f() , respectively: template <typename T> void g(T&& t) { if (is_lvalue_reference<T>::value) { f(t.x); } else { f(move(t.x)); } } The above code illustrate my intention but is not very scalable as the number of parameters grows. Is there a way make it work for perfect forwarding and make it scalable? template <typename T> void g(T&& t) { f(std::forward<T>(t).x); } I think this will work, although I'm not

How useful would Inheriting Constructors be in C++?

别来无恙 提交于 2019-11-28 05:47:29
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 everyone what inheriting constructors are: struct B { B(int); }; struct D : B { using B::B; }; Some vendors are proposing that with r-value references and variadic templates (perfect forwarding constructors), it would be trivial to provide a forwarding constructor in the inheriting class that would obviate inheriting constructors. For e.g.: struct D :

A failure to instantiate function templates due to universal (forward) reference to a templated type

╄→гoц情女王★ 提交于 2019-11-28 03:03:17
问题 Universal references (i.e. "forward references", the c++ standard name) and perfect forwarding in c++11 , c++14 , and beyond have many important advantages; see here, and here. In Scott Meyers' article referenced above (link), it is stated as a rule of thumb that: If a variable or parameter is declared to have type T&& for some deduced type T, that variable or parameter is a universal reference. Example 1 Indeed, using clang++ we see that the following code snippet will successfully compile

Purpose of perfect forwarding for Callable argument in invocation expression?

孤人 提交于 2019-11-28 01:51:43
In Scott Meyer's book Effective Modern C++ on page 167 (of the print version), he gives the following example: auto timeFuncInvocation = [](auto&& func, auto&&... params) { // start timer; std::forward<decltype(func)>(func)( std::forward<decltype(params)>(params)... ); // stop timer and record elapsed time; }; I completely understand the perfect forwarding of params , but it is unclear to me when perfect forwarding of func would ever be relevant. In other words, what are the advantages of the above over the following: auto timeFuncInvocation = [](auto&& func, auto&&... params) { // start timer

Is there a difference between universal references and forwarding references?

半腔热情 提交于 2019-11-27 21:03:56
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 <typename T> void f(T && t); I've often heard this referred to as a universal reference. I've also heard it been called a forwarding reference. Do they mean the same thing? Is it only a forwarding reference if the function body calls std::forward ? Do they mean the same thing? Universal reference was a term Scott Meyers coined to describe the concept of taking an rvalue reference to a cv-unqualified template

C++ Unified Assignment Operator move-semantics

亡梦爱人 提交于 2019-11-27 18:49:56
EDIT: solved see comments --don't know how to mark as solved with out an answer. After watching a Channel 9 video on Perfect Forwarding / Move semantics in c++0x i was some what led into believing this was a good way to write the new assignment operators. #include <string> #include <vector> #include <iostream> struct my_type { my_type(std::string name_) : name(name_) {} my_type(const my_type&)=default; my_type(my_type&& other) { this->swap(other); } my_type &operator=(my_type other) { swap(other); return *this; } void swap(my_type &other) { name.swap(other.name); } private: std::string name;

Perfect forwarding - what's it all about? [duplicate]

女生的网名这么多〃 提交于 2019-11-27 17:23:26
Possible Duplicate: Advantages of using forward Could someone please explain to me what perfect forwarding is about? http://www.justsoftwaresolutions.co.uk/cplusplus/rvalue_references_and_perfect_forwarding.html Why is this useful? Well, it means that a function template can pass its arguments through to another function whilst retaining the lvalue/rvalue nature of the function arguments by using std::forward. This is called "perfect forwarding", avoids excessive copying, and avoids the template author having to write multiple overloads for lvalue and rvalue references. Quoting Session

is any difference between std::forward<T> and std::forward<decltype(t)>?

妖精的绣舞 提交于 2019-11-27 14:35:12
问题 are these functions equivalent? template <class T> void foo(T && t) { bar(std::forward<T>(t)); } template <class T> void foo2(T && t) { bar(std::forward<decltype(t)>(t)); } template <class T> void foo3(T && t) { bar(std::forward(t)); } if they are, can I always use this macro for perfect forwarding? #define MY_FORWARD(var) std::forward<decltype(var)>(var) or just use bar(std::forward(t)); I believe foo2 and foo3 are same, but I found people are always use forward like foo , is any reason to

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

笑着哭i 提交于 2019-11-27 12:31:26
问题 (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 the exact same way but using a different name (that is, an alias). For example: template<class A, class B, class C> D fun(A a, B& b, C&& c){ ... } template<class E, class F> G fun(H<E> he, F& f){ ... } ... many other versions of fun Now suppose that

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

痴心易碎 提交于 2019-11-27 10:31:30
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; } void set_b(B const& b) { _b = b; } ... private: A _a; B _b; ... }; Setter functions of class X above can bind both to lvalue and to rvalue arguments. Depending on the actual argument, this might result in the creation of a temporary and will eventually result in a copy assignment; due to this, non-copiable types are not supported by this design. With C+