universal-reference

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+

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

爱⌒轻易说出口 提交于 2019-11-27 03:12:08
问题 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

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

夙愿已清 提交于 2019-11-27 01:16:23
问题 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

Is there a difference between universal references and forwarding references?

孤者浪人 提交于 2019-11-26 16:30:50
问题 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 ? 回答1: Do they mean the same thing? Universal reference was a term

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