rvalue-reference

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

﹥>﹥吖頭↗ 提交于 2019-12-03 00:55:02
I was hoping stringstream has a constructor that steals its initial content from a string&& . Do such inter-species "move constructors" generally not exist in the STL? If not, why not? 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> . For example the internal store could be a vector . And there was no ability to

Forwarding of return values. Is std::forward is needed?

拟墨画扇 提交于 2019-12-02 23:31:14
I am writing library which wraps a lot of functions and methods from other library. To avoid coping of return values I am applying std::forward like so: template<class T> T&& wrapper(T&& t) { f(t); // t passed as lvalue return std::forward<T>(t); } f returns void and takes T&& (or overloaded on valueness). Wrapper always returns wrappers's param and on returned value should preserve valuness of argument. Do I actually need to use std::forward in return ? Does RVO makes it superfluous? Does the fact that it is a reference (R or L) makes it superfluous? Is it needed if return is not last

Why is it not efficient to use a single assignment operator handling both copy and move assignment?

血红的双手。 提交于 2019-12-02 21:19:52
Here is an exercise from C++ Primer 5th Edition : Exercise 13.53: As a matter of low-level efficiency, the HasPtr assignment operator is not ideal. Explain why. Implement a copy-assignment and move-assignment operator for HasPtr and compare the operations executed in your new move-assignment operator versus the copy-and-swap version.(P.544) File hasptr.h : //! a class holding a std::string* class HasPtr { friend void swap(HasPtr&, HasPtr&); friend bool operator <(const HasPtr& lhs, const HasPtr& rhs); public: //! default constructor. HasPtr(const std::string &s = std::string()): ps(new std:

C++0x rvalue references and temporaries

北战南征 提交于 2019-12-02 20:39:37
(I asked a variation of this question on comp.std.c++ but didn't get an answer.) Why does the call to f(arg) in this code call the const ref overload of f ? void f(const std::string &); //less efficient void f(std::string &&); //more efficient void g(const char * arg) { f(arg); } My intuition says that the f(string &&) overload should be chosen, because arg needs to be converted to a temporary no matter what, and the temporary matches the rvalue reference better than the lvalue reference. This is not what happens in GCC and MSVC (edit: Thanks Sumant: it doesn't happen in GCC 4.3-4.5). In at

Passing/Moving parameters of a constructor in C++0x

泄露秘密 提交于 2019-12-02 20:23:45
If I have a constructor with n parameters such that any argument to that can be an rvalue and lvalue. Is it possible to do support this with move semantics for the rvalues without writing 2^n constructors for each possible rvalue/lvalue combination? You take each one by value, like this: struct foo { foo(std::string s, bar b, qux q) : mS(std::move(s)), mB(std::move(b)), mQ(std::move(q)) {} std::string mS; bar mB; qux mQ; }; The initialization of the function parameters by the argument will either be a copy-constructor or move-constructor. From there, you just move the function parameter values

Move Constructors and Static Arrays

落爺英雄遲暮 提交于 2019-12-02 20:21:01
I've been exploring the possibilities of Move Constructors in C++, and I was wondering what are some ways of taking advantage of this feature in an example such as below. Consider this code: template<unsigned int N> class Foo { public: Foo() { for (int i = 0; i < N; ++i) _nums[i] = 0; } Foo(const Foo<N>& other) { for (int i = 0; i < N; ++i) _nums[i] = other._nums[i]; } Foo(Foo<N>&& other) { // ??? How can we take advantage of move constructors here? } // ... other methods and members virtual ~Foo() { /* no action required */ } private: int _nums[N]; }; Foo<5> bar() { Foo<5> result; // Do stuff

Intuitive understanding of functions taking references of references [duplicate]

霸气de小男生 提交于 2019-12-02 19:31:18
Possible Duplicate: What does T&& mean in C++11? For some reason, this is eluding my intuition, and I cannot find any explanation on the internet. What does it mean for a C++ function to take a reference of a reference? For example: void myFunction(int&& val); //what does this mean?! I understand the idea of passing-by-reference, so void addTwo(int& a) { a += 2; } int main() { int x = 5; addTwo(x); return 0; } works and is intuitive to me. templatetypedef This is not a reference of a reference, but rather a new language feature called an rvalue reference that represents (informally) a

Are there any use cases for std::forward with a prvalue?

大兔子大兔子 提交于 2019-12-02 19:01:15
The most common usage of std::forward is to, well, perfect forward a forwarding (universal) reference, like template<typename T> void f(T&& param) { g(std::forward<T>(param)); // perfect forward to g } Here param is an lvalue , and std::forward ends up casting it to a rvalue or lvalue, depending on what the argument that bounded to it was. Looking at the definition of std::forward from cppreference.com I see that there is also a rvalue overload template< class T > T&& forward( typename std::remove_reference<T>::type&& t ); Can anyone give me any reason why the rvalue overload? I cannot see any

Use of rvalue reference members?

送分小仙女□ 提交于 2019-12-02 17:16:54
I was wondering what use an rvalue reference member has class A { // ... // Is this one useful? Foo &&f; }; Does it have any benefits or drawbacks compared to an lvalue reference member? What is a prime usecase of it? 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

Correct use of `= delete` for methods in classes

白昼怎懂夜的黑 提交于 2019-12-02 17:05:51
Is the following snipplet correct for un-defining all otherwise generated methods and constructors for a class? struct Picture { // 'explicit': no accidental cast from string to Picture explicit Picture(const string &filename) { /* load image from file */ } // no accidental construction, i.e. temporaries and the like Picture() = delete; // no copy Picture(const Picture&) = delete; // no assign Picture& operator=(const Picture&) = delete; // no move Picture(Picture&&) = delete; // no move-assign Picture& operator=(Picture&&) = delete; // return type correct? }; This deletes every default