Why does make_pair
and Class Template Argument Deduction (CTAD) not agree on which type to generate?
#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';
}
Output:
St4pairIiRiE // std::pair<int, int&>
St4pairIiSt17reference_wrapperIiEE // std::pair<int, std::reference_wrapper<int> >
Update:
Why do the deduction guides for std::pair
not include a guide for std::reference_wrapper
like make_pair
has an overload?
Becase
make_pair
is smart:This calls the overload unwrapping the
std::reference_wrapper<int>
:On the other hand, the the implicitly-generated deduction guides for
std::pair
take the types as-is.