error C2280 - attempting to reference a deleted function

Hi guys,

this piece of code

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Test
{
  Test() {};
  Test(const Test&) = delete;
  Test& operator=(const Test&) = delete;
};
int main(int argc, char* argv[])
{
  std::vector<Test> tests;
  tests.push_back(std::move(Test())); //gives error here

  return 0;
}


gives me error: error C2280: 'Test::Test(const Test &)': attempting to reference a deleted function

I know that I've explicitely deleted copy constructor, but I thought I could move instance of Test() to vector insteasd of copying it. What am I doing wrong? How to move Test() to vector?

Thank you.
I think I understand it now.

Deleting copy constructor explicitely caused compiler to not generate move constructor.
So calling std::move(Test()) does not call move constructor because there is no one.

But in that case, shouldn't compiler inform me that move constructor is missing?
Last edited on
That's right. If you want the compiler to generate the move constructor and the move assignment operator you'll need to say so explicitly.

1
2
3
4
5
6
7
8
struct Test
{
  Test() {};
  Test(const Test&) = delete;
  Test& operator=(const Test&) = delete;
  Test(Test&&) = default;
  Test& operator=(Test&&) = default;
};
Last edited on
> But in that case, shouldn't compiler inform me that move constructor is missing?

If only the copy constructor is provided, all argument categories select it (as long as it takes a reference to const, since rvalues can bind to const references), which makes copying the fallback for moving, when moving is unavailable.

https://en.cppreference.com/w/cpp/language/move_constructor
Thanks guys.
Topic archived. No new replies allowed.