In the following code, why is std::make_unique
necessary to initialize Foo::up_
?
Specifically, why doesn’t initializing Foo::up_
with new Bar(...)
— as in the commented-out code — work?
#include <iostream>
#include <memory>
enum E {
Y = 0,
Z = 1,
NUM_E
};
class Bar {
public: // Functions
Bar( const int& i, const int& j ) : i_(i), j_(j) { }
public: // Objects
int i_;
int j_;
};
class Foo {
public: // Functions
Foo();
void reset( const int& Yi, const int& Yj,
const int& Zi, const int& Zj );
public: // Objects
std::unique_ptr<Bar> up_[NUM_E];
};
Foo::Foo()
: up_{ std::make_unique<Bar>( 42, 43 ),
std::make_unique<Bar>( 44, 45 ) }
// : up_{ new Bar( 42, 43 ),
// new Bar( 44, 45 ) } // err: could not convert from Bar* to unique_ptr
{ }
void Foo::reset( const int& Yi, const int& Yj,
const int& Zi, const int& Zj ) {
up_[Y].reset( new Bar( Yi, Yj ) );
up_[Z].reset( new Bar( Zi, Zj ) );
}
int main( int argc, char* argv[] ) {
(void)argc;
(void)argv;
Foo foo;
std::cout << foo.up_[Y]->i_ << std::endl;
std::cout << foo.up_[Y]->j_ << std::endl;
std::cout << foo.up_[Z]->i_ << std::endl;
std::cout << foo.up_[Z]->j_ << std::endl;
foo.reset( 1, 2, 3, 4 );
std::cout << foo.up_[Y]->i_ << std::endl;
std::cout << foo.up_[Y]->j_ << std::endl;
std::cout << foo.up_[Z]->i_ << std::endl;
std::cout << foo.up_[Z]->j_ << std::endl;
return 0;
}
$ g++ --version && g++ --std=c++14 ./main.cpp && ./a.out
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
42
43
44
45
1
2
3
4
How does one work around this for C++11
, where std::make_unique
doesn’t appear to be available?
$ g++ --version && g++ --std=c++11 ./main.cpp && ./a.out
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
./main.cpp: In constructor ‘Foo::Foo()’:
./main.cpp:31:10: error: ‘make_unique’ is not a member of ‘std’
: up_{ std::make_unique<Bar>( 42, 43 ),
^~~
./main.cpp:31:30: error: expected primary-expression before ‘>’ token
: up_{ std::make_unique<Bar>( 42, 43 ),
^
./main.cpp:32:10: error: ‘make_unique’ is not a member of ‘std’
std::make_unique<Bar>( 44, 45 ) }
^~~
./main.cpp:32:30: error: expected primary-expression before ‘>’ token
std::make_unique<Bar>( 44, 45 ) }
^
3
Answers
If you do not have make_unique, you can just use the constructor of unique_ptr like this
https://godbolt.org/z/G9f5v1
1. The constructor
unique_ptr( pointer p )
is markedexplicit
, which prevents instances ofunique_ptr
from being constructed implicitly in array list initialization.2.
std::make_unique
is available since C++14.In C++11 a possible solution to both points is to construct an instance explicitly e.g.
std::unique_ptr<Bar>(new Bar( 42, 43 ))
.The reason why you can’t construct your array of
unique_ptr's
from array initializer of simple pointers is becauseunique_ptr
pointer constructor is explicit.To fix this issue, you could either call explicit constructors of
unique_ptr
s, but that’s quite boring and prone to issues which prompted addingmake_unique
in the first place. Instead, you can simple have your owncompat::make_unique
template function, which is literally 1 line: