Template argument deduction is a compiler attempt to deduce template arguments when some are omitted.
Questions tagged [template-argument-deduction]
692 questions
15
votes
1 answer
Partial template function specification in C++ works, but why?
I'm trying to find out whether partial specification of templated functions is part of the C++ standard, or whether this is something compiler specific.
By partial specification, I mean specifying only the types the compiler can't deduce. So if I…

James Freeman
- 153
- 4
14
votes
1 answer
C++20 designated initializers with templated types
How are designated initializers (C++20) supposed to work with CTAD?
This code works fine in gcc9.2, but fails with clang8
template
struct my_pair {
int_t first;
float_t…

nnolte
- 1,628
- 11
- 25
14
votes
3 answers
Can template deduction guides call constexpr functions?
I have my own fixed-size array type I want to be constexpr constructible from an std::initializer_list without having to explicitly define the size template argument.
I thought I'd be able to use a template deduction guide but it looks like it's not…

Blake Preston
- 176
- 6
14
votes
4 answers
Why didn't the C++17 standard bring partial class template argument deductions?
One of the places I hoped I would be able to use the new template argument deduction, was in construction of std::set's / std::maps / any other containers with custom comparators - my goal is to create a one-line statement, that will create an…

Kaznov
- 1,035
- 10
- 17
14
votes
1 answer
Class template argument deduction and default template parameters
The following stripped down code doesn't work with the latest clang++5 but is accepted by g++7:
template
struct wrapper;
template
struct wrapper
{
wrapper() = default;
//…

Morwenn
- 21,684
- 12
- 93
- 152
14
votes
2 answers
Why does deduction fail for std::set in GCC?
I have a std::set which allows deduction from an iterator range.
#include
#include
int main()
{
std::set s1 = {1,2,3,4};
std::set s2(s1.begin(), s1.end());
}
The above program failed to compile in GCC.
Why does deduction…

msc
- 33,420
- 29
- 119
- 214
14
votes
2 answers
Can constructor template cause ambiguity in the c++17 parameter deduction of class template
Consider a simple example:
template
struct foo {
template class TT>
foo(TT&&) {}
foo(foo&&){}
foo() {}
};
int main() {
foo f1(foo{}); //case 1.
foo f2(foo{}); //case…

W.F.
- 13,888
- 2
- 34
- 81
14
votes
2 answers
Template deduction guide for function?
I am trying to write some templated functions that accept either a std::basic_string or a char array from which the basic_string could be constructed.
My current solution is:
#include
template
void…

Boiethios
- 38,438
- 19
- 134
- 183
14
votes
4 answers
Implicit conversions with variadic templates
Consider two function calls
foo({"a", 1}, {"b", "value"});
foo({"a", 1}, {"b", "value"}, {"c", 1.0});
Is there a way to write function foo for arbitrary number of argument pairs?
I was thinking something along the lines
template

Blaz Bratanic
- 2,279
- 12
- 17
14
votes
1 answer
Template arguments deduction for parameter type of function pointer involving non-deduced parameter pack
This is similar to the question, but a more specific case. This time, no compiler work as expected.
template
struct nondeduced
{
using type = T;
};
template
using nondeduced_t = typename nondeduced::type;
template

Jamboree
- 5,139
- 2
- 16
- 36
14
votes
3 answers
template function call with empty angle brackets <>
I am confused with the below template behavior, where it compiles fine with the empty angle brackets (template without parameters) since syntactically, template<> is reserved to mark an explicit template specialization.
template void…

Ibrahim Quraish
- 3,889
- 2
- 31
- 39
14
votes
1 answer
Understanding SFINAE
As far as I know, SFINAE means substitution failures do not result in compilation errors, but just remove the prototype from the list of possible overloads.
What I do not understand: why is this SFINAE:
template struct…

nikolas
- 8,707
- 9
- 50
- 70
14
votes
2 answers
Template function type deduction and operator<<
When I compile the following code with MSVC++, I get an error:
struct A
{
template
void operator<<(T&& x)
{
}
};
void f()
{
}
int main()
{
A().operator<<( f ); // ok
A() << f; // error
return…

dsi
- 586
- 3
- 13
13
votes
1 answer
Type conflicts in non-type template argument deduction
#include
template
struct A { };
template
void f(A) {
}
int main() {
f(A{});
}
https://godbolt.org/z/n6bcj5rjM
The program is accepted by GCC and ICC in C++14 and C++17 mode,…

user17732522
- 53,019
- 2
- 56
- 105
13
votes
2 answers
Why is copy deduction candidate necessary as a separate deduction guide?
template struct A {
A(T);
A(const A&);
};
int main()
{
A x(42); // #1
A y = x; // #2
}
As I understand,T for #1 will be deduced using the implicit deduction guide
generated from the first ctor. Then x will be…

ledonter
- 1,269
- 9
- 27