std :: make_pair:无法将'ch'(类型'char')转换为'char&&'

时间:2013-02-26 18:18:23

标签: c++ c++11

以下代码有什么问题:

#include <ctime>
#include <vector>
#include <utility>
#include <algorithm>
#include <iostream>

int main()
{
    std::vector< std::pair< char, unsigned > > vec;

    for( unsigned i = 0; i < 100; ++i )
    {
        char ch = 0;
        unsigned number = 0;

        do {
            ch = i;
            number = i;

        } while( std::find( vec.begin(), vec.end(), std::make_pair< char, unsigned >( ch, number ) ) != vec.end() );

        std::cout << ch << number << '\n';

        vec.push_back( std::make_pair< char, unsigned >( ch, number ) );
    }
}

它可以很好地编译:

g++ test.cxx

但失败了:

$ g++ -std=c++11 test.cxx                                                                                                              /tmp
test.cxx: In function 'int main()':
test.cxx:21:98: error: no matching function for call to 'make_pair(char&, unsigned int&)'
test.cxx:21:98: note: candidate is:
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:65:0,
                 from /usr/include/c++/4.7/vector:61,
                 from test.cxx:3:
/usr/include/c++/4.7/bits/stl_pair.h:268:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
/usr/include/c++/4.7/bits/stl_pair.h:268:5: note:   template argument deduction/substitution failed:
test.cxx:21:98: note:   cannot convert 'ch' (type 'char') to type 'char&&'
test.cxx:25:69: error: no matching function for call to 'make_pair(char&, unsigned int&)'
test.cxx:25:69: note: candidate is:
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:65:0,
                 from /usr/include/c++/4.7/vector:61,
                 from test.cxx:3:
/usr/include/c++/4.7/bits/stl_pair.h:268:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
/usr/include/c++/4.7/bits/stl_pair.h:268:5: note:   template argument deduction/substitution failed:
test.cxx:25:69: note:   cannot convert 'ch' (type 'char') to type 'char&&'

1 个答案:

答案 0 :(得分:14)

<强> SOLUTION:

而不是以这种方式明确指定make_pair<>()的模板参数:

std::make_pair< char, unsigned >( ch, number )

让他们推断出来:

std::make_pair( ch, number )

<强>说明

本指南背后的基本原理可以通过定义std::make_pair<>()的方式找到,以及模板参数推导适用于通用引用的方式。从C ++ 11标准的第20.3.3 / 8-9段开始:

  

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

     

返回pair<V1, V2>(std::forward<T1>(x), std::forward<T2>(y));   其中V1和V2的确定如下:对于每个Uidecay<Ti>::typeTi。如果Vi等于X&,则每个Uireference_wrapper<X>,否则ViUi。 [示例:代替:

     

return pair<int, double>(5, 3.1415926); // explicit types

     

C ++程序可能包含:

     

return make_pair(5, 3.1415926); // types are deduced

     

- 结束示例]

此处,T1T2 意味着要推断。通过自己明确指定模板参数,您可以使用make_pair<>()的类型推导机制来生成正确的返回类型,从而强制实例化一个接受char的右值引用的函数和rvalue对unsigned的引用:

... make_pair(char&&, unsigned&&)

但是,您没有在输入中提供rvalues,因为chnumber是左值。这就是编译器所抱怨的。


<强> ALTERNATIVE:

另请注意,您可以隐式构建 std::pair个对象,这样就可以免于调用make_pair<>()

vec.push_back( { ch, number } );