universal-reference

C++ universal reference in constructor and return value optimization (rvo)

狂风中的少年 提交于 2019-12-01 16:55:42
Why does rvalue optimization not occur in classes with constructor with universal reference arguments? http://coliru.stacked-crooked.com/a/672f10c129fe29a0 #include <iostream> template<class ...ArgsIn> struct C { template<class ...Args> C(Args&& ... args) {std::cout << "Ctr\n";} // rvo occurs without && ~C(){std::cout << "Dstr\n";} }; template<class ...Args> auto f(Args ... args) { int i = 1; return C<>(i, i, i); } int main() { auto obj = f(); } Output: Ctr Ctr Dstr Ctr Dstr Dstr I believe that the problem is that instantiations of template<class ...Args> C(Args&& ... args) {std::cout << "Ctr

Passing a reference-to-function as a universal reference

岁酱吖の 提交于 2019-12-01 05:50:44
I'm struggling to understand what exactly happens when passing a reference-to-function to a function as a universal reference (what type is being deduced). Let's suppose we have a function foo that takes a param as a universal reference: template<typename T> void foo(T&& param) { std::cout << __PRETTY_FUNCTION__ << std::endl; } And then let's do the following: void(&f)(int) = someFunction; foo(f); The result will be: void foo(T&&) [with T = void (&)int] This is perfectly understandable: we are passing lvalue to our function foo, so the deduced type is void(&)int, and the type of the param will

Passing a reference-to-function as a universal reference

六眼飞鱼酱① 提交于 2019-12-01 03:27:13
问题 I'm struggling to understand what exactly happens when passing a reference-to-function to a function as a universal reference (what type is being deduced). Let's suppose we have a function foo that takes a param as a universal reference: template<typename T> void foo(T&& param) { std::cout << __PRETTY_FUNCTION__ << std::endl; } And then let's do the following: void(&f)(int) = someFunction; foo(f); The result will be: void foo(T&&) [with T = void (&)int] This is perfectly understandable: we

Why does this function return an lvalue reference given rvalue arguments?

醉酒当歌 提交于 2019-11-30 11:42:29
The following definition of a min function template <typename T, typename U> constexpr auto min(T&& t, U&& u) -> decltype(t < u ? t : u) { return t < u ? t : u; } has a problem: it seems that it's perfectly legal to write min(10, 20) = 0; This has been tested with Clang 3.5 and g++ 4.9. The solution is straightforward, just use std::forward to restore the "rvalue-ness" of the arguments, i.e. modify the body and the decltype to say t < u ? std::forward<T>(t) : std::forward<U>(u) However, I'm at a loss to explain why the first definition doesn't generate an error. Given my understanding of

Why does a range-based for statement take the range by auto&&?

こ雲淡風輕ζ 提交于 2019-11-30 11:25:42
A range-based for statement is defined in §6.5.4 to be equivalent to: { auto && __range = range-init; for ( auto __begin = begin-expr, __end = end-expr; __begin != __end; ++__begin ) { for-range-declaration = *__begin; statement } } where range-init is defined for the two forms of range-based for as: for ( for-range-declaration : expression ) => ( expression ) for ( for-range-declaration : braced-init-list ) => braced-init-list (the clause further specifies the meaning of the other sub-expressions) Why is __range given the deduced type auto&& ? My understanding of auto&& is that it's useful

Why does this function return an lvalue reference given rvalue arguments?

我的梦境 提交于 2019-11-29 17:26:25
问题 The following definition of a min function template <typename T, typename U> constexpr auto min(T&& t, U&& u) -> decltype(t < u ? t : u) { return t < u ? t : u; } has a problem: it seems that it's perfectly legal to write min(10, 20) = 0; This has been tested with Clang 3.5 and g++ 4.9. The solution is straightforward, just use std::forward to restore the "rvalue-ness" of the arguments, i.e. modify the body and the decltype to say t < u ? std::forward<T>(t) : std::forward<U>(u) However, I'm

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

What's the standard/official name for universal references?

╄→гoц情女王★ 提交于 2019-11-27 20:31:10
I know that if a variable or parameter is declared to have type T&& for some deduced type T , that variable or parameter is widely called a universal reference . The term universal reference was introduced by Scott Meyers in his original talk “Universal References in C++11” . However, I wonder what's the official/standard term for universal references . Overview It is known that since C++11, a parameter of type T&& is called an rvalue reference [ ISO/IEC 14882:2011 §8.3.2/p2 References [dcl.ref] ]. That is, unless T is a template parameter type or auto or a typedef for some lvalue reference