universal-reference

universal reference vs const reference priority?

a 夏天 提交于 2019-12-20 18:53:34
问题 When I consider the two following overloads: template <class... T> void f(const T&... x); template <class T> void f(const T& x); I have the guarantee that f(x) will always call the second function and will never lead to an ambiguity. In a sense the second version is universally prioritized compared to the first one for one argument whatever its type is. Now consider the situation where there is a universal reference and a const reference versions of a function: template <class T> void f(T&& x

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

拜拜、爱过 提交于 2019-12-19 16:06:13
问题 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 回答1: I

const applied to “universal reference” parameter

北战南征 提交于 2019-12-11 12:03:31
问题 I've stumbled upon Scott Mayers article on universal references, link. From what I understood universal reference, that is some type T&& can mean an rvalue or lvalue type in different contexts. For example: template<typename T> void f(T&& param); // deduced parameter type ⇒ type deduction; // && ≡ universal reference In the above example depending on template parameter the T&& can be either an lvalue or rvalue, that is, it depends on how we call f int x = 10; f(x); // T&& is lvalue (reference

Can a defaulted template type be universal reference?

我的未来我决定 提交于 2019-12-10 19:47:01
问题 In the following, is && a universal reference ? template <class Function = std::greater<int> > void f(Function&& f = Function()); 回答1: The term universal reference is a made up term by Scott Meyers to make a distinction between normal rvalue references, i.e. int&& and rvalue references in template code, i.e. T&& . This is important because of the reference collapsing rules that come into play with template code, so this term is used to aid in teaching. The reason it is called a unversal

Member function template with universal reference won't accept lvalues

微笑、不失礼 提交于 2019-12-10 18:40:38
问题 I've been trying to use a template member function to set a value inside of my class. I wanted to use a universal reference so that I could accept any variant of the correct type (e.g. T , T& , T&& , const T , const T& , const T&& ) However, it seems that my member function will only accept rvalues, unlike a free function accepting a universal reference. template <typename T> class Foo{ public: void memberURef(T&& t){ val = std::forward<T>(t); } private: T val; }; template <typename T> void

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

How to store universal references

怎甘沉沦 提交于 2019-12-05 04:51:34
I need to store universal references inside a class (I am sure the referenced values will outlive the class). Is there a canonical way of doing so? Here is a minimal example of what I have come up with. It seems to work, but I'm not sure if I got it right. template <typename F, typename X> struct binder { template <typename G, typename Y> binder(G&& g, Y&& y) : f(std::forward<G>(g)), x(std::forward<Y>(y)) {} void operator()() { f(std::forward<X>(x)); } F&& f; X&& x; }; template <typename F, typename X> binder<F&&, X&&> bind(F&& f, X&& x) { return binder<F&&, X&&>(std::forward<F>(f), std:

What kind of problems for not forwarding universal reference?

半世苍凉 提交于 2019-12-03 17:31:20
问题 As far as I know, in C++11, universal reference should always be used with std::forward , but I am not sure of what kind of problem can occur if std::forward is not used. template <T> void f(T&& x); { // What if x is used without std::forward<T>(x) ? } Could you provide some illustrations of problems that could occur in this situation ? 回答1: There is no such rule to always use std::forward with universal references . On the contrary, it can be dangerous to use std::forward all over the place

When not to use `auto&&`?

给你一囗甜甜゛ 提交于 2019-12-03 11:15:20
问题 auto&& mytup = std::make_tuple(9,1,"hello"); std::get<0>(mytup) = 42; cout << std::get<0>(mytup) << endl; Is there a copy/move involved (without RVO) when returning from make_tuple? Is it causing undefined behavior? I can both read write the universal reference. Can auto&& var = func() be used always instead of auto var = func() so that there is no copy/move? 回答1: Yes. Any return from a function that does not return a reference type may involve a copy/move. Eliding that is what RVO is about.

When not to use `auto&&`?

与世无争的帅哥 提交于 2019-12-03 01:42:00
auto&& mytup = std::make_tuple(9,1,"hello"); std::get<0>(mytup) = 42; cout << std::get<0>(mytup) << endl; Is there a copy/move involved (without RVO) when returning from make_tuple? Is it causing undefined behavior? I can both read write the universal reference. Can auto&& var = func() be used always instead of auto var = func() so that there is no copy/move? Yes. Any return from a function that does not return a reference type may involve a copy/move. Eliding that is what RVO is about. The object that your reference is bound to needs to be initialized somehow. No. why should it? The lifetime