rvalue-reference

What's the intention of forward-by-lvalue-reference constructor while a perfect forwarding constructor exists?

可紊 提交于 2019-12-08 17:38:14
问题 Let's take std::pair<T1, T2> as an example. It has the following two constructors: constexpr pair( const T1& x, const T2& y ); // #1 template< class U1, class U2 > constexpr pair( U1&& x, U2&& y ); // #2 It seems that #2 can handle all cases that #1 can handle (without worse performance), except for cases where an argument is a list-initializer. For example, std::pair<int, int> p({0}, {0}); // ill-formed without #1 So my question is: If #1 is only intended for list-initializer argument, since

Make callback accept temporary on VS 2010

十年热恋 提交于 2019-12-08 04:26:21
问题 I have a callback implementation using rvalue references to store arguments that works fine with gcc, but fails to compile in VS 2010 on some code. A short version: #include <iostream> #include <string> class B { public: virtual void execute() = 0; }; template<typename FuncType, typename ArgType> class F : public B { public: F(FuncType func, ArgType && arg) : f(func), arg(arg) {} void execute() { f(arg); } private: FuncType f; ArgType && arg; }; template<typename FuncType, typename ArgType> B

Get an rvalue when calling a getter method on an rvalue object

邮差的信 提交于 2019-12-08 02:04:52
问题 Suppose, I have the following code. There's a copy constructor in B which calls a method which copies the resources of a. Now I also have a move constructor. In this case, a should not be copied but just "steal" the resources from an existing a. Therefore, I also implemented an init taking an rvalue. But of course, when I try to call it with parameter b.a, this is an lvalue... Is there a way to call this method? class A{ A(const A&& a){ // 'steal' resources from a } void init(A& a){ // init

Storing rvalue references: should this work?

旧巷老猫 提交于 2019-12-07 11:03:55
问题 I'm testing my understanding of lvalue and rvalue references by intentionally trying to break things. So say there is this struct: struct FooBar { FooBar(int&& number) : rNumber(number) { } int& rNumber; }; and I create an instance FooBar obj(5) . Every attempt to read the reference variable returns the right result (5). The same happens if I use const int& instead of int&& . I noticed that replacing int with std::string and reading the reference returns an empty string, so I suspect it gives

Perfect Forwarding Variadic Template to Standard Thread

て烟熏妆下的殇ゞ 提交于 2019-12-07 05:29:58
问题 I'm trying to make a form of std::thread that puts a wrapper around the code executed in the thread. Unfortunately I can't get it to compile due likely to my poor understanding of rvalues and the Function templated type I'm trying to pass. Here's my code: #include <vector> #include <thread> #include <utility> void Simple2(int a, int b) {} template <typename Function, typename... Args> void Wrapper(Function&& f, Args&&... a) { f(std::forward<Args>(a)...); } class Pool { public: template

Lvalue reference constructor is called instead of rvalue reference constructor

左心房为你撑大大i 提交于 2019-12-07 04:21:50
问题 There is this code: #include <iostream> class F { public: F() = default; F(F&&) { std::cout << "F(F&&)" << std::endl; } F(F&) { std::cout << "F(F&)" << std::endl; } }; class G { F f_; public: G(F&& f) : f_(f) { std::cout << "G()" << std::endl; } }; int main(){ G g = F(); return 0; } The output is: F(F&) G() Why F(F&) constructor is called instead of F(F&&) constructor in constructor of class G ? The parameter for constructor of class G is F&& f which is rvalue reference but constructor for

Is there any case where a return of a RValue Reference (&&) is useful?

安稳与你 提交于 2019-12-07 04:01:05
问题 Is there a reason when a function should return a RValue Reference ? A technique, or trick, or an idiom or pattern? MyClass&& func( ... ); I am aware of the danger of returning references in general, but sometimes we do it anyway, don't we ( T& T::operator=(T) is just one idiomatic example). But how about T&& func(...) ? Is there any general place where we would gain from doing that? Probably different when one writes library or API code, compared to just client code? 回答1: There are a few

How are rvalues in c++ stored in memory?

自古美人都是妖i 提交于 2019-12-07 01:26:44
问题 Trying to learn lvalues , rvalues and memory allocation for them. So with a lot of learning materials there is a bit of chaos. An rvalue is a value that needs to exist only in bounds of a expression where it was created (until C++11 at least). So it has an address and block of memory that it occupies. But by definition we cannot get an address of rvalue , because it is a temporary object in contrast to an lvalue . But even before C++11 we were able to get an address of rvalue by returning it

Are temporary objects xvalues?

不想你离开。 提交于 2019-12-07 00:21:26
问题 I am currently writing my degree dissertation and it involves also some explaining of the theory behind C++11 which is really fine as C++ is my programming language of choice and the standard is more or less available for free (N3337) to get yourself lost in. Yet I have hit a wall while trying to explain the new xvalue category accurately and in detail. It is my understanding that a temporary object is always a xvalue but I cannot find any reference to this in the standard. It is my

Can an rvalue reference bind to a function?

风格不统一 提交于 2019-12-06 18:48:41
问题 I tested the following code with GCC, Clang, ICC and VS: void f() {} void g(void (&&)()) { } int main() { g(f); } As we can see, g takes an rvalue reference but f is an lvalue and, in general, rvalue references cannot be bound to lvalues. That's exactly what ICC complains about: error: an rvalue reference cannot be bound to an lvalue VS also gives an error but for another reason: error C2664: 'void h(void (__cdecl &&)(void))' : cannot convert parameter 1 from 'void (__cdecl *)(void)' to 'void