Bug 64147 - vector resize() fails for non-copyable type
Summary: vector resize() fails for non-copyable type
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-02 08:14 UTC by Eric Friedman
Modified: 2018-01-22 19:07 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Friedman 2014-12-02 08:14:33 UTC
Compiling the below program with -std=c++11 fails, "error: call to deleted constructor of 'Foo'".

This seems incorrect since C++11 explicitly added a new single argument vector resize method that is supposed to default-construct elements rather than copy them.

---

#include <vector>

class Foo {
 public:
  Foo() {}
  Foo(const Foo&) = delete;
};

int main() {
  std::vector<Foo> vec;
  vec.resize(42);  // error: call to deleted constructor of 'Foo'
}

---

By contrast, the following program using the vector size constructor works ok:

---

#include <vector>

class Foo {
 public:
  Foo() {}
  Foo(const Foo&) = delete;
};

int main() {
  std::vector<Foo> vec(42);  // compiles fine
}
Comment 1 Jonathan Wakely 2014-12-02 10:36:29 UTC
(In reply to Eric Friedman from comment #0)
> This seems incorrect since C++11 explicitly added a new single argument
> vector resize method that is supposed to default-construct elements rather
> than copy them.

It default constructs the new elements but when the vector reallocates its storage the existing elements need to be moved or copied to the new storage. Your type is neither movable nor copyable.

[vector.capacity]
 Requires: T shall be MoveInsertable and DefaultInsertable into *this.