Community
cancel
Showing results for 
Search instead for 
Did you mean: 
kingjsegmail_com
Beginner
63 Views

multimap support for unique_ptr

i am trying to build a multimap with unique_ptr and i am getting strange compilation errors in Linux

Example Code:

#include <iostream>
#include <memory>
#include <map>

using namespace std;

class Event {
public:
    Event (double time) : _time(time) {}
    double getTime () const { return _time; }
private:
    Event (Event const & e);
    void operator= (Event const &e);
    double _time;
};

class Calendar {
public:
    void addEvent (std::unique_ptr<Event> e) {
        double t = e->getTime(); // get time before trying the next line
        _events.insert(move(make_pair (t, move(e)))); // insert into multimap
    }
private:
    multimap <double, unique_ptr<Event>> _events;
};

int main () {
    unique_ptr<Event> e (new Event(1.0));

    Calendar c;
    c.addEvent (move(e));
}

above code compiles in OSX 10.10 with icpc 15.0.2 20150121

However, linux is another story:

% /opt/intel/bin/icpc --version && cat /etc/redhat-release && /opt/intel/bin/icpc main.cpp -std=c++11  -o main && ./main
icpc (ICC) 15.0.2 20150121
Copyright (C) 1985-2015 Intel Corporation.  All rights reserved.

CentOS release 6.6 (Final)
/usr/include/c++/4.4.7/bits/stl_pair.h(73): error: function "std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter> &) [with _Tp=Event, _Tp_Deleter=std::default_delete<Event>]" (declared at line 214 of "/usr/include/c++/4.4.7/bits/unique_ptr.h") cannot be referenced -- it is a deleted function
        _T2 second;                ///< @c second is a copy of the second object
            ^
          detected during:
            implicit generation of "std::pair<_T1, _T2>::pair(const std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>> &) [with _T1=const double, _T2=std::unique_ptr<Event, std::default_delete<Event>>]" at line 136 of "/usr/include/c++/4.4.7/bits/stl_tree.h"
            instantiation of class "std::pair<_T1, _T2> [with _T1=const double, _T2=std::unique_ptr<Event, std::default_delete<Event>>]" at line 136 of "/usr/include/c++/4.4.7/bits/stl_tree.h"
            instantiation of "std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args &&...) [with _Val=std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>, _Args=<const std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>> &>]" at line 111 of "/usr/include/c++/4.4.7/ext/new_allocator.h"
            instantiation of "void __gnu_cxx::new_allocator<_Tp>::construct(__gnu_cxx::new_allocator<_Tp>::pointer, _Args &&...) [with _Tp=std::_Rb_tree_node<std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>>, _Args=<const std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>> &>]" at line 395 of "/usr/include/c++/4.4.7/bits/stl_tree.h"
            instantiation of "std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args &&...) [with _Key=double, _Val=std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>, _KeyOfValue=std::_Select1st<std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>>, _Compare=std::less<double>, _Alloc=std::allocator<std::pair<const double, std::unique_ptr<Event,
                      std::default_delete<Event>>>>, _Args=<const std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>> &>]" at line 881 of "/usr/include/c++/4.4.7/bits/stl_tree.h"
            instantiation of "std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Base_ptr, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Base_ptr, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::value_type &) [with _Key=double, _Val=std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>,
                      _KeyOfValue=std::_Select1st<std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>>, _Compare=std::less<double>, _Alloc=std::allocator<std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>>]" at line 1200 of "/usr/include/c++/4.4.7/bits/stl_tree.h"
            instantiation of "std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_equal(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::value_type &) [with _Key=double, _Val=std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>, _KeyOfValue=std::_Select1st<std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>>, _Compare=std::less<double>,
                      _Alloc=std::allocator<std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>>]" at line 438 of "/usr/include/c++/4.4.7/bits/stl_multimap.h"
            instantiation of "std::multimap<_Key, _Tp, _Compare, _Alloc>::iterator std::multimap<_Key, _Tp, _Compare, _Alloc>::insert(const std::multimap<_Key, _Tp, _Compare, _Alloc>::value_type &) [with _Key=double, _Tp=std::unique_ptr<Event, std::default_delete<Event>>, _Compare=std::less<double>, _Alloc=std::allocator<std::pair<const double, std::unique_ptr<Event, std::default_delete<Event>>>>]" at line 21 of "main.cpp"

compilation aborted for main.cpp (code 2)

clearly the pair is trying to copy and this is triggering the error.  i have tried this all kinds of different ways and still can't get it to compile in linux.  suggestions?  i would think icpc would support unique_ptr with multimap right?

 

thanks!

 

0 Kudos
1 Reply
63 Views

Hi,

I can build the code with gcc 4.6.0, gcc 4.7.0, gcc 4.7.2, gcc 4.8.1, gcc 4.9.0...It failed with gcc 4.1.2, but not the same error as you have.

I'd suggest you to upgrade to a newer gcc version, as the code required C++11 support, older gcc may not support these features.

You may also verify whether the code built with your gcc (4.4.7) itself, it not, you have to upgrade your GCC (headers).

Let me know if it works.

Thanks,

Shenghong