jeudi 24 septembre 2015

std::tuple for non-copyable and non-movable object

I have a class with copy & move ctor deleted.

struct A
{
    A(int a):data(a){}
    ~A(){ std::cout << "~A()" << this << " : " << data << std::endl; }

    A(A const &obj) = delete;
    A(A &&obj) = delete;

    friend std::ostream & operator << ( std::ostream & out , A const & obj);

    int data;
};

And I want to create a tuple with objects of this class. But below does not compile.

auto p = std::tuple<A,A>(A{10},A{20}); 

Below does compile, but gives a surprising output.

CODE
-----------------
    main(){
       auto q = std::tuple<A&&,A&&>(A{100},A{200});
        std::cout << "q created\n";
    }

Output
---------
    ~A()0x22fe10 : 100
    ~A()0x22fe30 : 200
    q created

It means that dtor for objects is called as soon as tuple construction line ends. So, what is significance of a tuple of destroyed objects?

Aucun commentaire:

Enregistrer un commentaire