GCC 4.4 / 4.5 unique_ptr not working for unordered_set / unordered_map

Is there a place where I can confirm this? I am not sure if this is the problem of GCC or my code. For example, the following code does not compile:

#include <unordered_set> #include <memory> using namespace std; int main() { unordered_set<unique_ptr<int> > s; unique_ptr<int> p(new int(0)); s.insert(move(p)); return 0; } 

The error message is too large, and I do not want to indicate here. GCC version is 4.5.3, compilation flag is --std = gnu ++ 0x. Also checked on 4.4.5.

+6
source share
3 answers

Your code is correct. This is a known issue in GCC 4.5. This has been fixed in 4.6. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44436 . It also affects ordered containers (std :: map, std :: set, etc.). Probably the easiest way (with a slight decrease in performance) would be to use std :: shared_ptr instead of std :: unique_ptr.

+4
source

GCC 4.6.1 takes your code as is, and I donโ€™t see anything bad in it (i.e. the value_type of the associative container should be EmplaceInsertable and std::unique_ptr does not interfere with this). This is presumably a flaw in GCC 4.5.

+8
source

I can confirm that this is a problem with GCC 4.4.5. Attempting to insert unique_ptr in std :: set results in a long compiler error message, which indicates that some function in STL tried to copy unique_ptr:

error: remote function [unique_ptr copy ctor] ... is used here [g ++ - v4 / bit / stl_tree.h: 136].

The STL function in question is part of the internal tree structure of several STL classes, including std :: set. It is also in the ifdef file "__GXX_EXPERIMENTAL_CXX0X__", which supposedly means that GCC 4.4 does not officially support what we are trying to do.

If you do not want to upgrade to GCC 4.6, you can always wrap std :: vector and strategically check and remove duplicates at specific points in your code.

+3
source

Source: https://habr.com/ru/post/896841/


All Articles