1

I wanted to create an item into a map using C++17 (g++-6 or higher). The value_type of the map is a POD Bar which contains a non-copyable, non-moveable member of type Foo. By looking at stack-overflow, I found the following posts that told me to piecewise construct the map-item:

std::map emplace without copying value

Adding struct containing not copyable/moveable object to std::map

But I am currently not able to apply the instructions of both posts in order to construct Bar piecewise using the following example: (I get the compiler error below):

#include<string>
#include<map>
struct Foo
{
    Foo(int a, std::string b) {}
    Foo(const Foo&) = delete;
    Foo(Foo&&) = delete;
};

struct Bar
{
    Foo x;
    int y;
};

int main() {
    const int idx{0};
    const int a{1};
    const std::string b{"my"};
    const int y{2};
    std::map<int, Bar> m;
    m.emplace(std::piecewise_construct,
              std::forward_as_tuple(idx),
              std::forward_as_tuple({a, b}, y));
}

The compile error on g++-6 is as following:

tes.cpp: In function ‘int main()’:
tes.cpp:24:46: error: too many arguments to function ‘constexpr std::tuple<_Elements&& ...> std::forward_as_tuple(_Elements&& ...) [with _Elements = {}]’
               std::forward_as_tuple({a, b}, y));
                                              ^
In file included from /usr/include/c++/6/bits/stl_map.h:63:0,
                 from /usr/include/c++/6/map:61,
                 from tes.cpp:2:
/usr/include/c++/6/tuple:1403:5: note: declared here
     forward_as_tuple(_Elements&&... __args) noexcept
     ^~~~~~~~~~~~~~~~
/usr/include/c++/6/tuple: In instantiation of ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {const int&}; long unsigned int ..._Indexes1 = {0ul}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const int; _T2 = Bar]’:
/usr/include/c++/6/tuple:1579:63:   required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {const int&}; _Args2 = {}; _T1 = const int; _T2 = Bar]’
/usr/include/c++/6/ext/new_allocator.h:120:4:   required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, Bar>; _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, Bar> >]’
/usr/include/c++/6/bits/alloc_traits.h:475:4:   required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, Bar>; _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, Bar> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const int, Bar> > >]’
/usr/include/c++/6/bits/stl_tree.h:543:32:   required from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Bar>; _KeyOfValue = std::_Select1st<std::pair<const int, Bar> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Bar> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, Bar> >*]’
/usr/include/c++/6/bits/stl_tree.h:560:4:   required from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Bar>; _KeyOfValue = std::_Select1st<std::pair<const int, Bar> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Bar> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, Bar> >*]’
/usr/include/c++/6/bits/stl_tree.h:2149:64:   required from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_unique(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Bar>; _KeyOfValue = std::_Select1st<std::pair<const int, Bar> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Bar> >]’
/usr/include/c++/6/bits/stl_map.h:559:64:   required from ‘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>::emplace(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Key = int; _Tp = Bar; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Bar> >; 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 int, Bar> >]’
tes.cpp:24:47:   required from here
/usr/include/c++/6/tuple:1590:70: error: use of deleted function ‘Bar::Bar()’
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
                                                                      ^
tes.cpp:10:8: note: ‘Bar::Bar()’ is implicitly deleted because the default definition would be ill-formed:
 struct Bar
        ^~~
tes.cpp:10:8: error: no matching function for call to ‘Foo::Foo()’
tes.cpp:5:5: note: candidate: Foo::Foo(int, std::__cxx11::string)
     Foo(int a, std::string b) {}
     ^~~
tes.cpp:5:5: note:   candidate expects 2 arguments, 0 provided

How can I create an instance of Bar directly inside the map without the need of a copy- or move-construction of the class Foo?

byteunit
  • 991
  • 4
  • 15
  • A class that contains a non-copyable / non-movable member is not a POD. – eerorika Oct 28 '20 at 18:43
  • FWIW, `Bar` is not POD since `Foo` is not not a POD – NathanOliver Oct 28 '20 at 18:43
  • I think that you have to write `Foo{a, b}` instead of `{a,b}` otherwise the function `forward_as_tuple` cannot infer the type correctly (it would take it as `std::initializer_list` but `Foo` is not constructible from that) – n314159 Oct 28 '20 at 18:45
  • Possible duplicate: https://stackoverflow.com/q/54980297/2079303 – eerorika Oct 28 '20 at 18:49
  • @NathanOliver, i'm sorry i didn't know that it is no POD in this case. – byteunit Oct 28 '20 at 18:55
  • @n314159: Foo{a, b} doesn't work either – byteunit Oct 28 '20 at 18:56
  • Does this answer your question? [using c++ aggregate initialization in std::make\_shared](https://stackoverflow.com/questions/35298989/using-c-aggregate-initialization-in-stdmake-shared) (Actually, eerorika’s suggestion is even better.) – Davis Herring Oct 29 '20 at 01:45
  • for me it was clear that if you define a construct for Bar that forwards all parameters to Foo it will work, but I wanted to be sure that there is no other possibiltiy where I don't need to define a constructor (or a factory function). – byteunit Oct 30 '20 at 07:14

0 Answers0