C++ 0x中std :: make_tuple的问题

baa*_*rdk 5 c++ visual-studio-2010 c++11

为了使用Visual Studio 10编译以下程序,我得到了很多编译错误:

#include "stdafx.h"

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

int _tmain(int argc, _TCHAR* argv[])
{
    typedef std::tuple<std::string, std::string> key_t;
    typedef std::map<key_t, std::string> map_t;

    map_t the_map;

    auto k = std::make_tuple("one", "two");
    the_map[k] = "the value";

    auto  q = std::make_tuple("one", "two");

    auto  i = the_map.find(q);
    std::cout << i->second << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误1错误C2664:'std :: basic_string <_Elem,_Traits,_Ax> :: basic_string(const std :: basic_string <_Elem,_Traits,_Ax>&)':无法将参数1从'const key_t'转换为'const std :: basic_string <_Elem,_Traits,_Ax>&'c:\ program files(x86)\ microsoft visual studio 10.0\vc\include\tuple 127 1 tuple

来自这条线:

std::cout << i->second << std::endl;
Run Code Online (Sandbox Code Playgroud)

奇怪的是,至少从我的观点来看,如果我改变这些线:

auto k = std::make_tuple("one", "two");
the_map[k] = "the value";
Run Code Online (Sandbox Code Playgroud)

the_map[std::make_tuple("one", "two")] = "p";
Run Code Online (Sandbox Code Playgroud)

该程序编译.所以我的问题当然是为什么?我想这与make_tuple和移动语义有关 - 但我不明白是什么..

Tom*_*a17 5

显然,错误实际上来自于该行 the_map[k] = "the value";

在地图上使用[]运算符时,库会尝试创建std::pair<Key,Value>对象.在你的情况下,这变成了std::pair<std::tuple<std::string,std::string>,std::string>.

但是,如果使用中间变量k,则调用的std :: pair的构造函数为:(从标准库复制粘贴)

_Pair_base(const _Ty1x& _Val1, _Ty2x&& _Val2)
        : first(_Val1), second(_STD move(_Val2))
        {   // construct from specified values
        }
Run Code Online (Sandbox Code Playgroud)

这个构造函数试图复制你的key_t.不幸的是,MSVC++的元组实现目前被窃听并且副本无法编译(另请参阅:C++ 0x:是否允许使用元组的元组?)

我可以更多地诊断,因为这个实现不仅有问题,而且非常复杂.

Boost的元组应该可以工作,但是没有<运算符,所以你不能使用它们.

目前"最佳"的解决方案是写作 the_map.insert(std::make_pair(k, "the value"));