jeudi 30 juillet 2015

C++ 11 Explicilty defaulted user defined destructor being treated as user defined?

Based on explanation given in Does deleting a copy constructor or copy assignment operator count as "user declared"?, I concluded that explicitly defaulted destructor would not stop generation of implicit move constructor. But on testing the following code using g++ 4.8.4, it confirms that move constructors are not being generated.

#include <iostream>
#include <string>

using namespace std;
class test
{
public:
    test():m_data("IrRest"){}
    string print(){return m_data;}
    ~test()=default;
private:
    string m_data;
};

int main(int argc, char** argv, char** arge)
{
    test t1;
    test t2(std::move(t1));
    cout<<"t1::data "; 
    cout<<t1.print()<<endl;

    cout<<"t2:data ";
    cout<<t2.print()<<endl;
    return 0;
}

Output of the above program is:

t1::data IrRest

t2:data IrRest

But my expectation was that t1 would have been moved to t2. Hence the output should have been:

t1::data 
t2:data IrRest

If I remove the d'tor completely, then I get the above expected output. This probably suggests that explicitly defaulted d'tor counts as user defined as opposed to the explanation given under the link mentioned at the beginning of this query. I would appreciate it someone can throw more light on the above.

Aucun commentaire:

Enregistrer un commentaire