mercredi 26 août 2015

Should this simple structure have an implicit move constructor?

In this test:

#include <string>

struct thing {
    std::string name_;
};

class test {
    thing id_;
public:
    test(thing id) : id_{std::move(id)} {}
};

I would expect struct thing to have an implicit move constructor, so that class test can use std::move() to initialise its data member.

Clang version 3.4.1 gives this error:

error: no viable conversion from 'typename remove_reference<thing&>::type' (aka 'thing') to 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')

The problem can be solved by adding a move constructor to struct thing, which of course means that a converting constructor and an explicitly defaulted copy constructor also needs to be added.

I do not understand why I cannot move struct thing around implicitly.

Aucun commentaire:

Enregistrer un commentaire