Questions tagged [template-argument-deduction]

Template argument deduction is a compiler attempt to deduce template arguments when some are omitted.

692 questions
21
votes
2 answers

Class template argument deduction failed with derived class

#include template struct mypair : std::pair { using std::pair::pair; }; int main() { (void)std::pair(2, 3); // It works (void)mypair(2, 3); // It doesn't work } Is the above well formed? Is…
ABu
  • 10,423
  • 6
  • 52
  • 103
20
votes
1 answer

Auto return type of template and ambiguity

I have an overloaded template function: template auto overMax(T1 a, T2 b) { std::cout << __FUNCSIG__ << std::endl; return b < a ? a : b; } template RT overMax(T1 a, T2 b) { …
19
votes
2 answers

Substitution failure with `std::function` and previously deduced template parameter - why?

Consider the following code: template struct S { }; void g(S t); template void f(T, std::function)>); When attempting to invoke f(0, g); I get the following error: error: no matching function for call to…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
19
votes
1 answer

How can a compiler deduce this class template with forwarding reference?

I am looking into class template deduction available since C++17. Here are the code I would like to ask about: #include #include using std::endl; using std::cout; template struct MyAbs { template
Stephen
  • 609
  • 6
  • 12
19
votes
3 answers

Why do auto and template type deduction differ for braced initializers?

I understand that, given a braced initializer, auto will deduce a type of std::initializer_list, while template type deduction will fail: auto var = { 1, 2, 3 }; // type deduced as std::initializer_list template void f(T…
18
votes
2 answers

Why can template instances not be deduced in `std::reference_wrapper`s?

Suppose I have some object of type T, and I want to put it into a reference wrapper: int a = 5, b = 7; std::reference_wrapper p(a), q(b); // or "auto p = std::ref(a)" Now I can readily say if (p < q), because the reference wrapper has a…
18
votes
2 answers

Why isn't this call to a template ambiguous?

I declare two templates, the first converts the argument x from type T to type U and the second from type U to type T. If I call cast with 10, the compiler does not complain. I think both meet the requirements to be used and therefore there should…
17
votes
1 answer

Workaround for template argument deduction in non-deduced context

Consider the following code: #include template struct outer { struct inner {}; }; template std::ostream& operator<<(std::ostream & stream, typename outer::inner const& value) { …
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
17
votes
1 answer

g++ and clang++ different behaviour deducing variadic template `auto` values

Another "who's right between g++ and clang++?" This time I'm convinced it's a g++ bug, but I ask for a confirm from standard gurus. Given the following code template