Weird compiler error: Cannot convert parameter from 'int' to 'int &&'

前端 未结 4 1527
忘了有多久
忘了有多久 2020-12-09 04:14

What on earth is going on here?
I\'m trying to create a pair of an int and a string and I can create the pair if I use \"magic values\" but can

4条回答
  •  情深已故
    2020-12-09 04:28

    make_pair is usually used without specifying the template parameters explicitly. This is how it is meant to be used:

    num_text.push_back(std::make_pair(42, std::string("Smeg")));
    num_text.push_back(std::make_pair(42, text));
    num_text.push_back(std::make_pair(num, std::string("Smeg")));
    num_text.push_back(std::make_pair(num, text));
    num_text.push_back(std::make_pair(42, std::string("Smeg")));
    

    Alternatively, if you want the exact type:

    typedef decltype(num_text)::value_type value_type;
    num_text.push_back(value_type(42, std::string("Smeg")));
    num_text.push_back(value_type(42, text));
    num_text.push_back(value_type(num, std::string("Smeg")));
    num_text.push_back(value_type(num, text));
    num_text.push_back(value_type(42, std::string("Smeg")));
    

提交回复
热议问题