jeudi 9 avril 2020

Use case for std::forward_as__tuple

Cppreference gives for std::forward_as_tuple the following example (see here)

#include <iostream>
#include <map>
#include <tuple>
#include <string>

int main()
{
     std::map<int, std::string> m;

     m.emplace(std::piecewise_construct,
               std::forward_as_tuple(10),
               std::forward_as_tuple(20, 'a'));
     std::cout << "m[10] = " << m[10] << '\n';

     // The following is an error: it produces a
     // std::tuple<int&&, char&&> holding two dangling references.
     //
     // auto t = std::forward_as_tuple(20, 'a');
     // m.emplace(std::piecewise_construct, std::forward_as_tuple(10), t);
}

What is the benefit over simply writing

 m.emplace(std::make_pair(20,std::string(20,'a')));

Aucun commentaire:

Enregistrer un commentaire