Std :: tuple map for a tuple and using emplace

Consider the following code compiled with g ++ 7.0.1 (-std = C ++ 17):

#include <map>
#include <tuple>

int main()
{
    // Create an alias for a tuple of three ints
    using ThreeTuple=std::tuple<int,int,int>;
    // Create an alias for a map of tuple to tuple (of three ints)
    using MapThreeTupleToThreeTuple=std::map<ThreeTuple,ThreeTuple>;

    MapThreeTupleToThreeTuple m;

    // The following does NOT compile
    m.emplace({1,2,3},{4,5,6});

    // ..., and neither does this
    m.emplace(std::piecewise_construct,{1,2,3},{4,5,6});
}

I would have thought that there would be enough arguments initializer_listfor map::emplace()it and would have led to the insertion of the tuple key into the tuple value association, as indicated. Apparently, the compiler does not agree.

Of course, creating a tuple explicitly (i.e. ThreeTuple{1,2,3}instead of just {1,2,3}) and passing it to map::emplace()solves the problem, but why can't the initializer lists be passed directly to map::emplace()which will automatically redirect them to the tuple constructors?

+6
source share
2 answers

map::emplace()

, . emplace():

template< class... Args >
std::pair<iterator,bool> emplace( Args&&... args );

{1,2,3}. ++ 11, ++ 1z. std::initializer_list<T>, T .

m.emplace({1,2,3},{4,5,6});, :

std::pair<iterator,bool> emplace(key_type&&, mapped_type&&);
+5

AFAIK, ++ 17 . NathanOliver Barry, {1,2,3} , , . ThreeTuple , ..

m.emplace(std::piecewise_construct,
          std::forward_as_tuple(1,2,3),
          std::forward_as_tuple(4,5,6));

template<typename T1, typename T2>
template<typename... Args1, typename... Args2 >
std::pair<T1,T2>::pair(std::piecewise_construct_t,
                       std::tuple<Args1...>, std::tuple<Args2...>);

std::piecewise_construct

m.emplace(std::forward_as_tuple(1,2,3),
          std::forward_as_tuple(4,5,6));

( ++ 17, )

m.emplace(std::tuple(1,2,3), std::tuple(4,5,6));

m.emplace(ThreeTuple(1,2,3), ThreeTuple(4,5,6));

template<typename T1, typename T2>
std::pair<T1,T2>::pair(const&T1, const&T2);

, AFAIK , std::initializer_list<int> . , pair<ThreeTuple,ThreeTuple> (value_type ) .

+5

All Articles