Rvalue and Lvalue

hello everyone,
could anyone please explain what is the difference implementation of the below codes? I got that Lvalue is defined any entities that you can have the reference to it and in rvalue this is meaningless.

1
2
3
// assume we have class A
A a;    // a
a=A();  //A() is rvalue 


In addition to it A() still re-initialize a to stack?

Thanks
closed account (48T7M4Gy)
1
2
3
4
5
int main()
{
  int a;
  a = int;
}


Computer says, "No".

 In function 'int main()': 4:7: error: expected primary-expression before 'int'
> a=A(); //A() is rvalue
> In addition to it A() still re-initialize a to stack?

An anonymous temporary object of type A may or may not be created.
As-if rule: http://en.cppreference.com/w/cpp/language/as_if

If the default constructor and/or destructor of A has observable side effects, the observable behaviour of a=A(); would be "as-if" an anonymous temporary object of type A was constructed, the prvalue was move assigned, and the anonymous temporary object was then destroyed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

struct A
{
    A() { std::cout << "default constructor\n" ; } ;
    ~A() = default ;

    A( const A& ) { std::cout << "copy constructor\n" ; }
    A( A&& ) noexcept { std::cout << "move constructor\n" ; }

    A& operator= ( const A& ) { std::cout << "copy assignment\n" ; return *this ; }
    A& operator= ( A&& ) noexcept { std::cout << "move assignment\n" ; return *this ; }
};

int main()
{
    std::cout << "1. A a ; \n" ;
    A a ; // default constructor
    std::cout << "\n\n" ;

    std::cout << "2. a = A() ;\n" ;
    a = A() ; // default constructor (construct rvalue), move assignment
    std::cout << "\n\n" ;

    std::cout << "3. A a2(a) ;\n" ;
    A a2(a) ; // copy constructor
    std::cout << "\n\n" ;

    std::cout << "4. A a3{ A() } ;\n" ;
    A a3{ A() } ; // default constructor (copy-elision, even if there are side-effects, is permitted by the standard)
    std::cout << "\n\n" ;

    std::cout << "5. A a4( std::move(a3) ) ;\n" ;
    A a4( std::move(a3) ) ; // move constructor
    std::cout << "\n\n" ;

    std::cout << "6. a2 = a ; \n" ;
    a2 = a ; // copy assignment
    std::cout << "\n\n" ;

    std::cout << "7. a4 = std::move(a2) ;\n" ;
    a4 = std::move(a2) ;
    std::cout << "\n\n" ;
}

http://coliru.stacked-crooked.com/a/60438021a8227621
Last edited on
Thanks JLBorges!
but still I could not get will it be created in stack or heap?

thanks
> will it be created in stack or heap?

The C++ standard is only concerned about the lifetime of an object, and the duration for which the storage for an object would be available.

In this case, the object is an anonymous temporary, and
temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created, and if multiple temporary objects were created, they are destroyed in the order opposite to the order of creation.
http://en.cppreference.com/w/cpp/language/lifetime

Subject to the above requirement, an implementation is free to create a temporary object where ever it pleases; in practice, most implementations use a runtime stack for acquiring storage that can be reclaimed within a short period of time.
Topic archived. No new replies allowed.