Why is the compiler thinking I am trying to convert from an object of type Container
to an object of type MoveTest
in the following code? Why is the braced constructor being forwarded to the move_things
variable? Replacing the {}
with ()
of course solves the problem
#include <iostream>
#include <array>
using namespace std;
static constexpr int NUMBER{2};
class MoveTest {
public:
MoveTest(const MoveTest&) {
cout << "MoveTest(const MoveTest&)" << endl;
}
MoveTest(MoveTest&&) {
cout << "MoveTest(MoveTest&&)" << endl;
}
MoveTest() {
cout << "MoveTest()" << endl;
}
};
class Container {
public:
Container() = default;
Container(Container&&) = default;
Container(const Container&) = default;
std::array<MoveTest, NUMBER> move_things;
};
int main() {
Container container;
__attribute__((unused)) Container another_one{std::move(container)};
return 0;
}
The error I get is the following
Could not convert ‘std::move<Container&>((* & container))’ from ‘std::remove_reference<Container&>::type {aka Container}’ to ‘MoveTest`
Does this mean that the uniform initialization syntax is no longer an option for any class with an std::array
?
The compiler and version I am using is
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
来源:https://stackoverflow.com/questions/38254106/unneeded-conversion-happening-on-stdmove