Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

noncopyable, but movable container

I have a problem with this program:

struct A {};

int main()
{
  ::std::vector< ::std::unique_ptr<A> > v;

  ::std::cout << ::std::is_copy_constructible<decltype(v)>{} << ::std::endl;

  //decltype(v) w(v);

  return 0;
}

This outputs:

1

Yet, if I uncomment the commented line, the program will fail to compile. Do you think, it is a bug in the standard, that ::std::is_copy_constructible<decltype(v)>{} evaluates to true and where in the standard? Should, for example, the metafunction be fixed, or should the container delete it's copy constructor, if the value_type is not copyable?

EDIT: I suppose I should clarify why this is important. Say, you have a class template variant, that contains a container class template instantiated with a non-copyable value_type. The variant could SFINAE away methods that copy the container and avoid compile errors, but since it receives wrong information from the STL, it can't. As a result of this problem, I've had to write a special moving_variant class template, that only moves, never copies, whereas it could/should be possible to have a single variant class template.

like image 534
user1095108 Avatar asked Feb 19 '14 11:02

user1095108


1 Answers

is_copy_constructible is defined in terms of is_constructible, which is true if an expression like this is well-formed:

T t(create<const T&>())

In the case of vector<unique_ptr>, this is well-formed, since vector declares a suitable copy constructor. The constructor can't be instantiated since it uses a deleted function; but templates aren't instantiated when used in an unevaluated context like this.

like image 90
Mike Seymour Avatar answered Oct 26 '22 08:10

Mike Seymour