Unneeded conversion happening on std::move

不打扰是莪最后的温柔 提交于 2019-12-10 11:44:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!