reference_wrapper:make_pair VS类模板参数推导(CTAD)

Jon*_*nas 5 c++ templates template-argument-deduction c++17

为什么make_pair和Class Template Argument Deduction(CTAD)不同意生成哪种类型?

#include <iostream>
#include <functional>
#include <utility>
#include <typeinfo>

int main() {
    int myInt = 5;
    std::reference_wrapper<int> myIntRef = myInt;
    auto myPair = std::make_pair(myInt, myIntRef);
    std::pair My2ndPair(myInt, myIntRef);
    std::cout << typeid(myPair).name() << '\n';
    std::cout << typeid(My2ndPair).name() << '\n';
}
Run Code Online (Sandbox Code Playgroud)

输出:

St4pairIiRiE                       // std::pair<int, int&>
St4pairIiSt17reference_wrapperIiEE // std::pair<int, std::reference_wrapper<int> >
Run Code Online (Sandbox Code Playgroud)

更新:

对于扣除导游为什么std::pair不包括指南std::reference_wrapper一样make_pair的过载?

YSC*_*YSC 6

Becase make_pair聪明:

std::reference_wrapper<int> myIntRef = myInt;
auto myPair = std::make_pair(myInt, myIntRef);
Run Code Online (Sandbox Code Playgroud)

这会调用过载解开std::reference_wrapper<int>:

template<class T1, class T2>
  constexpr pair<unwrap_ref_decay_t<T1>, unwrap_ref_decay_t<T2>> make_pair(T1&& x, T2&& y);
Run Code Online (Sandbox Code Playgroud)

另一方面,隐式生成的 演绎指南用于std::pair按原样取类型.

  • 任何想法为什么演绎指南不"聪明"? (2认同)