关于VS2012使用make_pair编译提示“error C2664: “std::make_pair”: 不能将参数 1 从“int”转换为“int &&””



参照微软官方回复:

#include 
#include 
#include 
#include 
using namespace std;

int main() {
    string ht = "hello";
    pair ps;

#if defined(FIX1)
    ps = pair(1, ht);
#elif defined(FIX2)
    ps = make_pair(1, ht);
#else
    ps = make_pair(1, ht);
#endif

    cout << ps.first << " " << ps.second << endl;
}

C:\Temp>cl /EHsc /nologo /W4 kitty.cpp && kitty
kitty.cpp
kitty.cpp(16) : error C2664: 'std::make_pair' : cannot convert parameter 2 from 'std::string' to 'std::string &&'
        You cannot bind an lvalue to an rvalue reference

C:\Temp>cl /EHsc /nologo /W4 kitty.cpp /DFIX1 && kitty
kitty.cpp
1 hello

C:\Temp>cl /EHsc /nologo /W4 kitty.cpp /DFIX2 && kitty
kitty.cpp
1 hello

Both FIX1 and FIX2 construct a temporary pair, which is then converted during assignment to pair.

I personally recommend FIX2.

https://connect.microsoft.com/VisualStudio/feedback/details/691756/std-make-pair-error-in-vc11

你可能感兴趣的:(关于VS2012使用make_pair编译提示“error C2664: “std::make_pair”: 不能将参数 1 从“int”转换为“int &&””)