mercredi 31 mai 2017

Inserting into a map that has atomic key and value types in C++

In the following snippet:

#include <map>
#include <atomic>
#include <utility>

int main()
{

    std::map<std::atomic<int>, std::atomic<int>, std::greater<std::atomic<int>>> a_map;

    std::atomic<int> a_key;
    a_key.store(5);
    std::atomic<int> a_val;
    a_val.store(566);

    a_map.insert(std::make_pair(a_key, a_val));
}

Edit: This question was marked duplicate by a user, but the other question was for a class. I only have a std::map. Can someone please explain what is wrong here??

Why is the insert line giving a long list of cryptic errors?

g++ -std=c++11 test.cpp 
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/stl_tree.h:63,
                 from /usr/include/c++/5/map:60,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_pair.h: In instantiation of ‘constexpr std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = std::atomic<int>; _T2 = std::atomic<int>]’:
/usr/include/c++/5/bits/stl_pair.h:281:72:   required from ‘constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = std::atomic<int>&; _T2 = std::atomic<int>&; typename std::__decay_and_strip<_T2>::__type = std::atomic<int>; typename std::__decay_and_strip<_Tp>::__type = std::atomic<int>]’
test.cpp:15:45:   required from here
/usr/include/c++/5/bits/stl_pair.h:113:31: error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
       : first(__a), second(__b) { }
                               ^
In file included from test.cpp:2:0:
/usr/include/c++/5/atomic:612:7: note: declared here
       atomic(const atomic&) = delete;
       ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/stl_tree.h:63,
                 from /usr/include/c++/5/map:60,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:113:31: error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
       : first(__a), second(__b) { }
                               ^
In file included from test.cpp:2:0:
/usr/include/c++/5/atomic:612:7: note: declared here
       atomic(const atomic&) = delete;
       ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/stl_tree.h:63,
                 from /usr/include/c++/5/map:60,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_pair.h: In instantiation of ‘constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = std::atomic<int>&; _T2 = std::atomic<int>&; typename std::__decay_and_strip<_T2>::__type = std::atomic<int>; typename std::__decay_and_strip<_Tp>::__type = std::atomic<int>]’:
test.cpp:15:45:   required from here
/usr/include/c++/5/bits/stl_pair.h:281:72: error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(std::pair<_T1, _T2>&&) [with _T1 = std::atomic<int>; _T2 = std::atomic<int>]’
       return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
                                                                        ^
/usr/include/c++/5/bits/stl_pair.h:128:17: note: ‘constexpr std::pair<_T1, _T2>::pair(std::pair<_T1, _T2>&&) [with _T1 = std::atomic<int>; _T2 = std::atomic<int>]’ is implicitly deleted because the default definition would be ill-formed:
       constexpr pair(pair&&) = default;
                 ^
/usr/include/c++/5/bits/stl_pair.h:128:17: error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
In file included from test.cpp:2:0:
/usr/include/c++/5/atomic:612:7: note: declared here
       atomic(const atomic&) = delete;
       ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/stl_tree.h:63,
                 from /usr/include/c++/5/map:60,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:128:17: error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
       constexpr pair(pair&&) = default;
                 ^
In file included from test.cpp:2:0:
/usr/include/c++/5/atomic:612:7: note: declared here
       atomic(const atomic&) = delete;
       ^
test.cpp: In function ‘int main()’:
test.cpp:15:46: error: no matching function for call to ‘std::map<std::atomic<int>, std::atomic<int>, std::greater<std::atomic<int> > >::insert(std::pair<std::atomic<int>, std::atomic<int> >)’
     a_map.insert(std::make_pair(a_key, a_val));
                                              ^
In file included from /usr/include/c++/5/map:61:0,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_map.h:612:7: note: candidate: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::atomic<int>; _Tp = std::atomic<int>; _Compare = std::greater<std::atomic<int> >; _Alloc = std::allocator<std::pair<const std::atomic<int>, std::atomic<int> > >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator = std::_Rb_tree_iterator<std::pair<const std::atomic<int>, std::atomic<int> > >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const std::atomic<int>, std::atomic<int> >]
       insert(const value_type& __x)
       ^
/usr/include/c++/5/bits/stl_map.h:612:7: note:   no known conversion for argument 1 from ‘std::pair<std::atomic<int>, std::atomic<int> >’ to ‘const value_type& {aka const std::pair<const std::atomic<int>, std::atomic<int> >&}’
/usr/include/c++/5/bits/stl_map.h:620:9: note: candidate: template<class _Pair, class> std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(_Pair&&) [with _Pair = _Pair; <template-parameter-2-2> = <template-parameter-1-2>; _Key = std::atomic<int>; _Tp = std::atomic<int>; _Compare = std::greater<std::atomic<int> >; _Alloc = std::allocator<std::pair<const std::atomic<int>, std::atomic<int> > >]
         insert(_Pair&& __x)
         ^
/usr/include/c++/5/bits/stl_map.h:620:9: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/stl_map.h:616:32: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
       template<typename _Pair, typename = typename
                                ^
/usr/include/c++/5/bits/stl_map.h:633:7: note: candidate: void std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::initializer_list<std::pair<const _Key, _Tp> >) [with _Key = std::atomic<int>; _Tp = std::atomic<int>; _Compare = std::greater<std::atomic<int> >; _Alloc = std::allocator<std::pair<const std::atomic<int>, std::atomic<int> > >]
       insert(std::initializer_list<value_type> __list)
       ^
/usr/include/c++/5/bits/stl_map.h:633:7: note:   no known conversion for argument 1 from ‘std::pair<std::atomic<int>, std::atomic<int> >’ to ‘std::initializer_list<std::pair<const std::atomic<int>, std::atomic<int> > >’
/usr/include/c++/5/bits/stl_map.h:662:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, const value_type&) [with _Key = std::atomic<int>; _Tp = std::atomic<int>; _Compare = std::greater<std::atomic<int> >; _Alloc = std::allocator<std::pair<const std::atomic<int>, std::atomic<int> > >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::atomic<int>, std::atomic<int> > >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const std::atomic<int>, std::atomic<int> > >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const std::atomic<int>, std::atomic<int> >]
       insert(const_iterator __position, const value_type& __x)
       ^
/usr/include/c++/5/bits/stl_map.h:662:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/5/bits/stl_map.h:673:9: note: candidate: template<class _Pair, class> std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, _Pair&&) [with _Pair = _Pair; <template-parameter-2-2> = <template-parameter-1-2>; _Key = std::atomic<int>; _Tp = std::atomic<int>; _Compare = std::greater<std::atomic<int> >; _Alloc = std::allocator<std::pair<const std::atomic<int>, std::atomic<int> > >]
         insert(const_iterator __position, _Pair&& __x)
         ^
/usr/include/c++/5/bits/stl_map.h:673:9: note:   template argument deduction/substitution failed:
test.cpp:15:32: note:   cannot convert ‘std::make_pair<std::atomic<int>&, std::atomic<int>&>(a_key, a_val)’ (type ‘std::pair<std::atomic<int>, std::atomic<int> >’) to type ‘std::map<std::atomic<int>, std::atomic<int>, std::greater<std::atomic<int> > >::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const std::atomic<int>, std::atomic<int> > >}’
     a_map.insert(std::make_pair(a_key, a_val));
                                ^
In file included from /usr/include/c++/5/map:61:0,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_map.h:688:9: note: candidate: template<class _InputIterator> void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = std::atomic<int>; _Tp = std::atomic<int>; _Compare = std::greater<std::atomic<int> >; _Alloc = std::allocator<std::pair<const std::atomic<int>, std::atomic<int> > >]
         insert(_InputIterator __first, _InputIterator __last)
         ^
/usr/include/c++/5/bits/stl_map.h:688:9: note:   template argument deduction/substitution failed:
test.cpp:15:46: note:   candidate expects 2 arguments, 1 provided
     a_map.insert(std::make_pair(a_key, a_val));

Aucun commentaire:

Enregistrer un commentaire