Template argument deduction is a compiler attempt to deduce template arguments when some are omitted.
Questions tagged [template-argument-deduction]
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)
{
…

amplifier
- 1,793
- 1
- 21
- 55
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…

KnowItAllWannabe
- 12,972
- 8
- 50
- 91
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…

Kerrek SB
- 464,522
- 92
- 875
- 1,084
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…

wic
- 362
- 3
- 14
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 class Cnt,
typename ... Types,
Types ...…

max66
- 65,235
- 10
- 71
- 111
17
votes
2 answers
C++17 Partial Deduction Guide
I am trying to write a deduction guide, that only detects one of many typename's from given constructor argument and requires user to enter int size manually
template
struct Board
{
array, size>…

Eren
- 316
- 1
- 9
17
votes
1 answer
Variadic deduction guide not taken by g++, taken by clang++ - who is correct?
Consider the following code:
template
struct list
{
template
list(Args...)
{
static_assert(sizeof...(Types) > 0);
}
};
template
list(Args...) -> list;
int…

Vittorio Romeo
- 90,666
- 33
- 258
- 416
17
votes
2 answers
How to provide deduction guide for nested template class?
According to [temp.deduct.guide/3]:
(...) A deduction-guide shall be declared in the same scope as the
corresponding class template and, for a member class template, with
the same access. (...)
But below example doesn't seem to compile in both…

W.F.
- 13,888
- 2
- 34
- 81
16
votes
1 answer
Deduction guides, templates and subobjects: which compiler is right?
Consider the following snippet:
struct S {
S() {}
template
struct T {
T(B &&) {}
};
template
T(B &&) -> T;
};
int main() {
S::T t{0};
}
Clang accepts it while GCC rejects the code…

skypjack
- 49,335
- 19
- 95
- 187
15
votes
2 answers
Perfect forwarding in constructors (C++17)
Consider the following code
struct A {
A(int id) : id_ { id } {}
A(const A& rhs) { std::cout << "cctor from " +
std::to_string(rhs.id_) << std::endl; }
A(A&& rhs) { std::cout << "mctor from " +
std::to_string(rhs.id_) <<…

plexando
- 1,151
- 6
- 22
15
votes
1 answer
Why can't the compiler deduce the template parameter when used with a conversion operator?
Consider the following code:
#include
template
struct wrapper {
T value;
};
struct foo {
operator wrapper() {
return{10};
}
};
int main() {
foo f;
wrapper w = f; // error
std::pair p =…

Rakete1111
- 47,013
- 16
- 123
- 162