universal-reference

Is this a universal reference? Does std::forward make sense here?

让人想犯罪 __ 提交于 2021-02-07 05:20:20
问题 Consider this snippet of code, which uses the common idiom of having a function template construct an instance of a class template specialized on a deduced type, as seen with std::make_unique and std::make_tuple , for example: template <typename T> struct foo { std::decay_t<T> v_; foo(T&& v) : v_(std::forward<T>(v)) {} }; template <typename U> foo<U> make_foo(U&& v) { return { std::forward<U>(v) }; } In the context of Scott Meyers' "universal references", the argument to make_foo is a

Is this a universal reference? Does std::forward make sense here?

蓝咒 提交于 2021-02-07 05:20:14
问题 Consider this snippet of code, which uses the common idiom of having a function template construct an instance of a class template specialized on a deduced type, as seen with std::make_unique and std::make_tuple , for example: template <typename T> struct foo { std::decay_t<T> v_; foo(T&& v) : v_(std::forward<T>(v)) {} }; template <typename U> foo<U> make_foo(U&& v) { return { std::forward<U>(v) }; } In the context of Scott Meyers' "universal references", the argument to make_foo is a

Is this a universal reference? Does std::forward make sense here?

二次信任 提交于 2021-02-07 05:18:43
问题 Consider this snippet of code, which uses the common idiom of having a function template construct an instance of a class template specialized on a deduced type, as seen with std::make_unique and std::make_tuple , for example: template <typename T> struct foo { std::decay_t<T> v_; foo(T&& v) : v_(std::forward<T>(v)) {} }; template <typename U> foo<U> make_foo(U&& v) { return { std::forward<U>(v) }; } In the context of Scott Meyers' "universal references", the argument to make_foo is a

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

三世轮回 提交于 2020-01-10 17:48:40
问题 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

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

坚强是说给别人听的谎言 提交于 2020-01-09 07:13:49
问题 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 . 回答1: 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

Template deduction/overload resolution favors T&& over const T&

為{幸葍}努か 提交于 2019-12-24 16:54:57
问题 I have a pair of function templates defined like so: template<typename CollectionType> Foo<CollectionType> f(const CollectionType& v) { return Foo<CollectionType>(v); // copies v into a member variable } template<typename CollectionType> Foo<CollectionType> f(CollectionType&& v) { return Foo<CollectionType>(std::move(v)); // moves v into a member variable } If I call f as below: std::vector<int> v; f(v); The VC++ compiler favors the && overload, apparently because it is less specialized. I

How to store universal references

情到浓时终转凉″ 提交于 2019-12-22 04:47:07
问题 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,