0
template<typename T1, typename T2>
void printPair(const std::pair<T1, T2> &p) {
    std::cout << p.first << ", " << p.second << std::endl;
}

int main() {
    printPair({1, 1});
    return 0;
}

When I try to compile that code, the compiler is unable to infer the template arguments to the function printPair. If I remove the template code and change the parameter to std::pair<int,int> then the code compiles fine. Or I can keep the template and change the call to printPair(std::make_pair(1, 1)) and it also works fine.

Why can't the template arguments be deduced without using make_pair?

user1000039
  • 785
  • 1
  • 7
  • 19
  • 1
    Highly related: https://stackoverflow.com/questions/17582667/why-do-auto-and-template-type-deduction-differ-for-braced-initializers – NathanOliver Jul 13 '17 at 14:04
  • Possible duplicate of [Is it possible to infer template parameters of tuple from brace-type initialization?](https://stackoverflow.com/questions/16703838/is-it-possible-to-infer-template-parameters-of-tuple-from-brace-type-initializat) – Passer By Jul 13 '17 at 14:48
  • 1
    `{1, 1}` has no type. The only valid deduced type is `std::initializer_list`. – Jarod42 Jul 13 '17 at 15:21

0 Answers0