相关疑难解决方法(0)

带有指定模板参数的C++ 11 make_pair无法编译

我正在使用启用了-std = c ++ 11的g ++ 4.7(后面的一个快照).我试图编译一些现有的代码库,一个失败的案例让我感到困惑.

如果有人能解释发生了什么,我将不胜感激.

这是代码

#include <utility>
#include <iostream>
#include <vector>
#include <string>

int main ( )
{
    std::string s = "abc";

    // 1 ok
    std::pair < std::string, int > a = std::make_pair ( s, 7 );

    // 2 error on the next line
    std::pair < std::string, int > b = std::make_pair < std::string, int > ( s, 7 );

    // 3 ok
    std::pair < std::string, int > d = std::pair < std::string, int > ( …
Run Code Online (Sandbox Code Playgroud)

c++ templates g++ rvalue-reference c++11

78
推荐指数
1
解决办法
2万
查看次数

奇怪的编译器错误:无法将参数从'int'转换为'int &&'

这到底是怎么回事?
我正在尝试创建一对an int和a string,如果我使用"魔术值"但我似乎无法传递变量,我可以创建该对.

std::vector<std::pair<int, std::string> > num_text;

std::string text = "Smeg";
int num = 42;

// Works fine
num_text.push_back(std::make_pair<int, std::string>(42, std::string("Smeg")));  

// Cannot convert parameter 2 from 'std::string' to 'std::string &&'
num_text.push_back(std::make_pair<int, std::string>(42, text));

// Cannot convert parameter 1 from 'int' to 'int &&'
num_text.push_back(std::make_pair<int, std::string>(num, std::string("Smeg")));

// Cannot convert parameter 1 from 'int' to 'int &&'
num_text.push_back(std::make_pair<int, std::string>(num, text));

// Works fine again
num_text.push_back(std::make_pair<int, std::string>(42, std::string("Smeg")));
Run Code Online (Sandbox Code Playgroud)

我正在使用VS 2012并粘贴了一些用VS 2008编写的代码.无法想象它与它有什么关系,但原始(2008)代码没有问题.

我觉得有点不能训练这里发生的事情,但我能说什么,我只是不明白.

c++

20
推荐指数
3
解决办法
1万
查看次数

标签 统计

c++ ×2

c++11 ×1

g++ ×1

rvalue-reference ×1

templates ×1