简体   繁体   中英

Why is std::make_unique necessary to initialize member std::unique array, and how to work around for C++11?

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 ) }
                              ^

1. The constructor unique_ptr( pointer p ) is marked explicit , which prevents instances of unique_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 eg 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 because unique_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 adding make_unique in the first place. Instead, you can simple have your own compat::make_unique template function, which is literally 1 line:

template<class T, class... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

If you do not have make_unique, you can just use the constructor of unique_ptr like this

Foo::Foo()
  : up_{ std::unique_ptr<Bar>(new Bar( 42, 43 )),
         std::unique_ptr<Bar>(new Bar( 44, 45 )) }
{ }

https://godbolt.org/z/G9f5v1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM