6

以下代码有什么问题:

#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&&'
4

1 回答 1

14

解决方案:

而不是以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);

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

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

C++ 程序可能包含:

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

—<em>结束示例]

在这里,T1并且T2被推断出来的。通过自己显式指定模板参数,您正在使用make_pair<>()' 的类型推导机制来生成正确的返回类型,从而强制实例化一个接受对 和 的右值引用char和对的右值引用的函数unsigned

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

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


选择:

另请注意,您可以隐式构造一个std::pair对象,从而完全避免调用make_pair<>()

vec.push_back( { ch, number } );
于 2013-02-26T18:21:41.743 回答