forwarding-reference

Can an identity alias template be a forwarding reference?

核能气质少年 提交于 2019-11-30 16:34:26
问题 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> &&'

When should I std::forward a function call?

不问归期 提交于 2019-11-30 07:12:41
A code snippet I saw in Effective Modern C++ has a clever implementation of the instrumentation rationale to create a function timer : auto timeFuncInvocation = [](auto&& func, auto&&... params) { start timer; std::forward<decltype(func)>(func)( std::forward<decltype(params)>(params)...); stop timer and record elapsed time; }; My question is about std::forward<decltype(func)>(func)(... To my understanding, we are actually casting the function to its original type, but why is this needed? It looks like a simple call would do the trick. Are there any other cases where we use perfect forwarding

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

我的未来我决定 提交于 2019-11-30 05:32:16
问题 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

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

Deadly 提交于 2019-11-30 04:51:56
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::decay_t<String>, std::string>::value>> void set_name(String &&name) noexcept( std::is_nothrow_assignable

When should I std::forward a function call?

眉间皱痕 提交于 2019-11-29 09:05:13
问题 A code snippet I saw in Effective Modern C++ has a clever implementation of the instrumentation rationale to create a function timer : auto timeFuncInvocation = [](auto&& func, auto&&... params) { start timer; std::forward<decltype(func)>(func)( std::forward<decltype(params)>(params)...); stop timer and record elapsed time; }; My question is about std::forward<decltype(func)>(func)(... To my understanding, we are actually casting the function to its original type, but why is this needed? It

Why does std::move take a forward reference?

元气小坏坏 提交于 2019-11-29 04:06:28
The implementation of std::move basically looks like this: template<typename T> typename std::remove_reference<T>::type&& move(T&& t) { return static_cast<typename std::remove_reference<T>::type&&>(t); } Note that the parameter of std::move is a universal reference (also known as a forwarding reference, but we're not forwarding here). That is, you can std::move both lvalues and rvalues: std::string a, b, c; // ... foo(std::move(a)); // fine, a is an lvalue foo(std::move(b + c)); // nonsense, b + c is already an rvalue But since the whole point of std::move is to cast to an rvalue, why are we

Why adding `const` makes the universal reference as rvalue

此生再无相见时 提交于 2019-11-29 02:31:44
问题 I have been reading about the universal references in Scott's last master piece about the c++11 and 14 with that being said despite an argument assigned to either lvalue or an rvalue type reference parameter there is something in between called universal reference which could deduced to either l/rvalue based on the type trait of an argument that passed . I could understand what makes the parameter as an universal reference but the one thing that doesn't clear to me is why adding const to the

Does an lvalue argument prefer an lvalue reference parameter over a universal reference?

自闭症网瘾萝莉.ら 提交于 2019-11-28 09:47:39
While playing with universal references, I came across this instance where clang and gcc disagree on overload resolution. #include <iostream> struct foo {}; template<typename T> void bar(T&) { std::cout << "void bar(T&)\n"; } template<typename T> void bar(T&&) { std::cout << "void bar(T&&)\n"; } int main() { foo f; bar(f); // ambiguous on gcc, ok on clang } gcc reports the call above is ambiguous. However, clang selects the T& overload and compiles successfully. Which compiler is wrong, and why? Edit: Tested the same code on VS2013 Preview, and it agrees with clang; except Intellisense, which

What does `auto && e` do in range-based for-loops?

瘦欲@ 提交于 2019-11-28 06:19:34
Assuming my current rule when programming with range-based loops says Use for(auto const &e :...) or for(auto &e:...) when possible over for(auto a: ...) . I base this on my own experience and this question for example. But after reading about the new terse for loops I wonder, should I not replace my & in my rule with && ? As written here this looks like the Meyers' Universal References . So, I ask myself, should my new rule either be Use for(auto const &&e :...) or for(auto &&e:...) when possible ... or does that not always work and therefore should rather be the quite complicated one Check

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