unique ptr - c++11 Automatic Initialization (brace list) of std::unorderer_map with std::unique_ptr within -
i trying implement const unorderer_map class menber variable, getting stuck 2 hours around implementation, cannot figure out doing wrong:
#include <iostream> #include <unordered_map> #include <memory> namespace nstest { struct pointerclassimp { }; struct classcontainner { classcontainner():map_{{0,std::unique_ptr<pointerclassimp>(new pointerclassimp)}} {} const std::unordered_map<unsigned, std::unique_ptr<pointerclassimp>> map_; }; } int main() { } this code not compiles. can have help? propouses of code create @ construction time const map use in class. thx in advance compilation ouptut:
/main.cpp:12:81: required here /usr/include/c++/4.8/bits/hashtable_policy.h:195:39: error: use of deleted function ‘constexpr std::pair<_t1, _t2>::pair(const std::pair<_t1, _t2>&) [with _t1 = const unsigned int; _t2 = std::unique_ptr<nstest::pointerclassimp>]’ compiler gcc (ubuntu 4.8.4-2ubuntu1~14.04.3)
seems, following link have answer question: can list-initialize vector of move-only type?
the problem seems implicit copy in construccion. have change type of "std::unique_ptr" -> "std::shared_ptr" allows copy compiler allow me constuct const unorderer_map. not type of smart pointer want. there construccion brace-list process allow create unorderer_map std::unique_ptr ? thx in advance
Comments
Post a Comment