forwarding-reference

What's the correct `enable_if` constraint on perfect forwarding setter?

那年仲夏 提交于 2019-12-18 11:46:47
问题 Herb Sutter's Back to the Basics! Essentials of Modern C++ presentation at CppCon discussed different options for passing parameters and compared their performance vs. ease of writing/teaching. The 'advanced' option (providing the best performance in all the cases tested, but too difficult for most developers to write) was perfect forwarding, with the example given (PDF, pg. 28): class employee { std::string name_; public: template <class String, class = std::enable_if_t<!std::is_same<std:

Make a code “forwarding referencable”

混江龙づ霸主 提交于 2019-12-13 19:07:48
问题 I opened this post about forwarding reference, this is a (hopefully) MCVE code: #include <functional> #include <vector> using namespace std; struct MultiMemoizator { template <typename ReturnType, typename... Args> ReturnType callFunction(std::function<ReturnType(Args...)> memFunc, Args&&... args) { } }; typedef vector<double> vecD; vecD sort_vec (const vecD& vec) { return vec; } int main() { vecD vec; std::function<vecD(const vecD&)> sortFunc(sort_vec); MultiMemoizator mem; mem.callFunction

Lambda capturing rvalue reference by reference

我的梦境 提交于 2019-12-10 22:09:34
问题 Is below code standard-correct? (godbolt) I.e. by-ref capturing a forwarding reference that represents a temporary, and returning the resulting lambda by-value from the function, within the same expression . Of course storing the lambda for later use would make it contain a dangling reference, but I'm referring to the exact usage inside main . The doubts I'm having relate to this SO answer and potentially this language defect. Specifically there is one daunting comment that says "the

C++11 perfect forwarding and reference collapsing

左心房为你撑大大i 提交于 2019-12-10 11:49:27
问题 Consider this code: template<typename T> void foo(T&& param){ //In this case && is called universal reference std:string tmp = std::forward<string>(param); } My question is if universal reference type can be deduced why do I still need to call forward ? Why without forwarding tmp's correct c'tor won't be called even if T's type was deduced. My second question is about reference collapsing rules: A& && becomes A& A&& && becomes A&& so according this rules and taking in account universal

Proper use of universal references

守給你的承諾、 提交于 2019-12-10 03:43:57
问题 Before c++11, I used to write code like this: // Small functions void doThingsWithA(const A& a) { // do stuff } void doThingsWithB(const B& b) { // do stuff } void doThingsWithC(const C& c) { // do stuff } // Big function void doThingsWithABC(const A& a, const B& b, const C& c) { // do stuff doThingsWithA(a); doThingsWithB(b); doThingsWithC(c); // do stuff } But now, with move semantics, it may become interesting (at least in some cases) to allow my functions to take rvalue references as

Perfect forwarding in a lambda?

做~自己de王妃 提交于 2019-12-07 00:48:37
问题 With a function, one can write: template <class T> void f(T&& x) {myfunction(std::forward<T>(x));} but with a lambda, we don't have T : auto f = [](auto&& x){myfunction(std::forward</*?*/>(x));} How to do perfect-forwarding in a lambda? Does decltype(x) work as the type in std::forward ? 回答1: The canonical way to forward a lambda argument that was bound to a forwarding reference is indeed with decltype : auto f = [](auto&& x){ myfunction(std::forward<decltype(x)>(x)); } // ^^^^^^^^^^^ 回答2: My

auto&& variable's are not rvalue reference

浪子不回头ぞ 提交于 2019-12-05 18:22:31
Why auto&& is not rvalue reference? Widget&& var1 = Widget(); // rvalue reference auto&& var2 = var1; //var2 not rvalue reference below are rvalue reference example void f(Widget&& param); // rvalue reference Widget&& var1 = Widget(); // rvalue reference Why var2 is not rvalue reference but f and var2 are rvalue references? Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto using the rules for template argument deduction from a function call (see template argument deduction#Other contexts for details). The keyword auto may

Perfect forwarding in a lambda?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 05:53:31
With a function, one can write: template <class T> void f(T&& x) {myfunction(std::forward<T>(x));} but with a lambda, we don't have T : auto f = [](auto&& x){myfunction(std::forward</*?*/>(x));} How to do perfect-forwarding in a lambda? Does decltype(x) work as the type in std::forward ? Kerrek SB The canonical way to forward a lambda argument that was bound to a forwarding reference is indeed with decltype : auto f = [](auto&& x){ myfunction(std::forward<decltype(x)>(x)); } // ^^^^^^^^^^^ My favorite idiom for this is: auto f = [](auto&& x){myfunction(decltype(x)(x));} which I read as " x as

How can I create a macro which uses a value multiple times, without copying it?

巧了我就是萌 提交于 2019-11-30 21:04:45
I'd like to create a macro which unpacks a pair into two local variables. I'd like to not create a copy of the pair if it's just a variable, which this would accomplish: #define UNPACK_PAIR(V1, V2, PAIR) \ auto& V1 = PAIR.first; \ auto& V2 = PAIR.second; UNPACK_PAIR(one, two, x); However, I'd also like it to not evaluate the expression it's given multiple times, e.g. this should only call expensive_computation() once: UNPACK_PAIR(one, two, expensive_computation()); If I do: #define UNPACK_PAIR_A(V1, V2, PAIR) \ auto tmp = PAIR; \ auto& V1 = tmp.first; \ auto& V2 = tmp.second; then it works for

Can an identity alias template be a forwarding reference?

余生颓废 提交于 2019-11-30 17:23:01
Consider the following snippet below: template <class T> using identity = T; template <class T> void foo(identity<T>&&) {} int main() { int i{}; foo(i); } i is an lvalue, hence if foo declares a forwarding reference parameter, it should compile. However, if identity<T>&& is turned to be int&& , it should raise an error instead. The code compiles in GCC 6.0.0 ( demo ). The code fails to compile in Clang 3.7.0 ( demo ) with error message: error: no known conversion from 'int' to 'identity<int> &&' (aka 'int &&') for 1st argument Which one is right? bogdan Consider this code: template<class T>