Use of rvalue reference members?

I’ve seen one very motivating use case for rvalue reference data members, and it is in the C++0x draft: template<class… Types> tuple<Types&&…> forward_as_tuple(Types&&… t) noexcept; Effects: Constructs a tuple of references to the arguments in t suitable for forwarding as arguments to a function. Because the result may contain references to temporary variables, a program … Read more

Avoid exponential grow of const references and rvalue references in constructor

Actually, this is the precise reason why perfect forwarding was introduced. Rewrite the constructor as template <typename L, typename O> LinearClassifier(L && loss, O && optimizer) : _loss(std::forward<L>(loss)) , _optimizer(std::forward<O>(optimizer)) {} But it will probably be much simpler to do what Ilya Popov suggests in his answer. To be honest, I usually do it this … Read more

Why doesn’t `std::stringstream::stringstream(std::string&&)` exist?

There’s history, which is disappointing. But also a future that looks bright. When move semantics went into C++11, it was huge, controversial, and overwhelming. I wanted to be able to move strings into and out of stringstream. However the politics at the time demanded that the internal store did not have to be a basic_string<charT>. … Read more

How to make template rvalue reference parameter ONLY bind to rvalue reference?

You can restrict T to not be an lvalue reference, and thus prevent lvalues from binding to it: #include <type_traits> struct OwnershipReceiver { template <typename T, class = typename std::enable_if < !std::is_lvalue_reference<T>::value >::type > void receive_ownership(T&& t) { // taking file descriptor of t, and clear t } }; It might also be a good … Read more

Why user-defined move-constructor disables the implicit copy-constructor?

I’ve upvoted ildjarn’s answer because I found it both accurate and humorous. 🙂 I’m providing an alternate answer because I’m assuming because of the title of the question that the OP might want to know why the standard says so. background C++ has implicitly generated copy members because if it didn’t, it would’ve been still-born … Read more

Can I typically/always use std::forward instead of std::move?

The two are very different and complementary tools. std::move deduces the argument and unconditionally creates an rvalue expression. This makes sense to apply to an actual object or variable. std::forward takes a mandatory template argument (you must specify this!) and magically creates an lvalue or an rvalue expression depending on what the type was (by … Read more

Overload resolution between object, rvalue reference, const reference

What are the rules here? As there is only one parameter, the rule is that one of the three viable parameter initializations of that parameter must be a better match than both the other two. When two initializations are compared, either one is better than the other, or neither is better (they are indistinguishable). Without … Read more

tech