#include <iostream>
#include <map>
#include <string>
#include <tuple>
int main()
{
std::map<std::string, std::string> myMap;
myMap.emplace(
std::piecewise_construct , // pair CTOR selector
std::forward_as_tuple("four_c"), // pair first CTOR args
// {string(string&& other)}
std::forward_as_tuple(4, 'c' )); // pair second CTOR args
// {string(size_type count,
// CharT ch)}
std::cout << myMap["four_c"] << " ";
// The '5' and 'c' are dangling references after the first semicolon.
auto myTupleWithRvalueReferences = std::forward_as_tuple(5, 'c');
#if 0 // The following line does not compile,
// because of the '5' and 'c' dangling references above.
myMap.emplace(
std::piecewise_construct ,
std::forward_as_tuple("five_c"),
myTupleWithRvalueReferences );
#endif
std::cout << myMap["five_c"] << " ";
int count = 6 ;
char ch = 'c';
auto myTupleWithLvalueReferences = std::forward_as_tuple(count, ch);
myMap.emplace(
std::piecewise_construct ,
std::forward_as_tuple("six_c"),
myTupleWithLvalueReferences );
std::cout << myMap["six_c"] << " ";
std::cout << std::endl;
return 0;
}
// Output: cccc cccccc
Reference: http://en.cppreference.com/w/cpp/utility/tuple/forward_as_tuple
Monday, November 27, 2017
C++11: std::forward_as_tuple()
C++11 added the template function std::forward_as_tuple(), which creates a tuple that can be forwarded. If the arguments are rvalues, then the tuple members are rvalue references, otherwise the tuple members are lvalue members. Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment