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

前端 未结 4 1515
忘了有多久
忘了有多久 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")));
    
    0 讨论(0)
  • 2020-12-09 04:38

    Reference says:

    template< class T1, class T2 >
    std::pair<T1,T2> make_pair( T1 t, T2 u );           (until C++11)
    
    template< class T1, class T2 >
    std::pair<V1,V2> make_pair( T1&& t, T2&& u );       (since C++11)
    

    Note that the return type is different. It also says:

    The deduced types V1 and V2 are std::decay::type and std::decay::type (the usual type transformations applied to arguments of functions passed by value) unless application of std::decay results in std::reference_wrapper for some type X, in which case the deduced type is X&.

    So in fact, since 2008 (I mean Visual C++ 2008), the semantics of the function make_pair has changed. You could either remove the template parameters from std::make_pair and let it deduce the type, or use std::pair's constructor if you need to make pairs of specific type:

    num_text.push_back(std::make_pair(num, text));               // deduced type
    num_text.push_back(std::pair<int, std::string>(num, text));  // specific type
    

    The reason for the compile error is that you have specified the types to be int (as T1) and std::string (as T2), and therefore the function expects T1 && and T2 &&. See this answer for why that's a problem.

    0 讨论(0)
  • 2020-12-09 04:47

    make_pair<T1,T2> does not make a pair of type pair<T1,T2>, but rather deduces a suitable pair of reference types from its arguments to allow perfect forwarding. It's specified as

    template <class T1, class T2>
    pair<V1, V2> make_pair(T1&& x, T2&& y);
    

    for some suitable reference types V1 and V2. This only works if the argument types are deduced, so that && can decay to an lvalue reference if necessary. By specifying the template parameters explicitly, they are no longer deduced, and so the function arguments can only be rvalues.

    The solution is to let the compiler deduce the types:

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

    If you need to make a pair of a particular type, don't use make_pair, just make a pair

    // Works, but perhaps with more copying than you want.
    num_text.push_back(std::pair<int, std::string>(num, text));   
    
    0 讨论(0)
  • 2020-12-09 04:48

    Now std::make_pair is defined the following way in the C++ Standard

    template <class T1, class T2>
    

    see below make_pair(**T1&&, T2&&**);

    You could write simpler without using std::make_pair

    num_text.push_back( { 42, text } ); .

    0 讨论(0)
提交回复
热议问题