perfect-forwarding

Capturing perfectly-forwarded variable in lambda

别等时光非礼了梦想. 提交于 2019-11-26 15:39:50
问题 template<typename T> void doSomething(T&& mStuff) { auto lambda([&mStuff]{ doStuff(std::forward<T>(mStuff)); }); lambda(); } Is it correct to capture the perfectly-forwarded mStuff variable with the &mStuff syntax? Or is there a specific capture syntax for perfectly-forwarded variables? EDIT: What if the perfectly-forwarded variable is a parameter pack? 回答1: Is it correct to capture the perfectly-forwarded mStuff variable with the &mStuff syntax? Yes, assuming that you don't use this lambda

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

左心房为你撑大大i 提交于 2019-11-26 15:13:11
问题 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

What&#39;s the difference between std::move and std::forward

冷暖自知 提交于 2019-11-26 11:31:06
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 std::move takes an object and allows you to treat it as a temporary (an rvalue). Although it isn't a semantic requirement, typically a function accepting a reference to an rvalue will invalidate it. When you see std::move , it indicates that the value of the object should not be used afterwards, but you can still assign a new value and continue using it. std:

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

折月煮酒 提交于 2019-11-25 23:05:26
问题 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 this. What exactly does it mean? At first glance, it appears to be a double reference (like the C-style double pointers T** var ), but I\'m having a hard time thinking of a use case for that. 回答1: It declares an rvalue reference (standards proposal doc).

Advantages of using forward

混江龙づ霸主 提交于 2019-11-25 22:56:06
问题 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 <typename T1, typename T2> void outer(T1&& t1, T2&& t2) { inner(std::forward<T1>(t1), std::forward<T2>(t2)); } 回答1: You have to understand the forwarding problem. You can read the entire problem in detail, but I'll summarize. Basically, given the