Template argument deduction is a compiler attempt to deduce template arguments when some are omitted.
Questions tagged [template-argument-deduction]
692 questions
8
votes
2 answers
Why can't I create a template function with an optional UnaryPredicate argument?
I'm trying to create a templated function with an optional argument and I'm having trouble understanding why the compile fails. This is my test (contrived) code:
#include
#include
template
int…

kshenoy
- 1,726
- 17
- 31
8
votes
1 answer
Clang claims that `member reference base type 'X' is not a structure or union`, but X is a structure template with deduced parameters
Consider following code:
template struct X
{
X(T) {}
void foo() {}
};
template struct Y
{
int object = 0;
void bar()
{
X(object).foo();
}
};
Live on gcc.godbold.org
GCC 8.2 compiles it,…

HolyBlackCat
- 78,603
- 9
- 131
- 207
8
votes
1 answer
Variadic template type deduction crashes compilers if there is a substitution on deducible type
I think I hit a template type deduction bug in all the compilers, but before reporting it I want to be sure I did not miss something.
Consider an example:
#include
template
void…

Nikita Kniazev
- 3,728
- 2
- 16
- 30
8
votes
2 answers
Template non-type parameter deduction
Is it possible to deduce template value (not type) for a c++17 function?
The function foo:
template
int foo()
{
return (I);
}
Can be called via:
foo<5>();
And will return 5.
Template types can be deduced through the type of a function…

user7119460
- 1,451
- 10
- 20
8
votes
1 answer
Template argument deduction failure with new expression
I'm working on a variadic class template but I cannot use it with a new expression without specifying the template arguments (I don't want to). I reduced the problem to the following code sample:
template
struct Foo
{
Foo(T p)
…

Zejj
- 83
- 5
8
votes
1 answer
Is it abuse to deduce parameters of parent template when passing pointer to constexpr function in the scope of a class
Minimal example I got is a bit complicated:
struct A { };
template
struct Parent { };
template
constexpr int operator*(A, Parent*) { return N; }
template
using ptr = T*;
template
struct Other { };
template

W.F.
- 13,888
- 2
- 34
- 81
8
votes
1 answer
Is it guaranteed that template template parameter invoke user provided deduction guides
Consider an example:
#include
#include
template class TT> //#1
struct Foo {
static void foo() {
static_assert(std::is_same_v>);
}
};
template

W.F.
- 13,888
- 2
- 34
- 81
8
votes
1 answer
Is "template argument deduction for class templates" supposed to deduce empty parameter packs for variadic class templates?
The "Template argument deduction for class templates" proposal (P0091R2) contains the following example:
template struct X { X(Ts...) };
X x1{1}; // OK X
X x11; // OK X<>
(Apart from the fact that the constructor definition is…

Vittorio Romeo
- 90,666
- 33
- 258
- 416
8
votes
1 answer
Can C++17's "template argument deduction" for class templates deduce local types?
P0091R3 ("Template argument deduction for class templates") was recently added to gcc trunk and can be tested on wandbox.
Something I had in mind was the possibility of using it to implement a
"scope guard" in very few lines of code:
scope_guard…

Vittorio Romeo
- 90,666
- 33
- 258
- 416
8
votes
1 answer
Do template structs need std::decay when using forwarding references in C++17?
In C++17, it will be possible to instantiate objects without specifying the template types. Basically, this code would compile:
std::pair p(2, 4.5); // deduces to std::pair p(2, 4.5);
std::tuple t(4, 3, 2.5); // same as auto t =…

Mário Feroldi
- 3,463
- 2
- 24
- 49
8
votes
2 answers
Template arguments deduction for function parameter pack followed by other parameters
Is the deduction for f1 and f2 ill-formed?
template
void f1(T..., U){}
template
void f2(T..., int){}
int main()
{
f1(1);
f2(1);
return 0;
}
g++ accepts both, clang only accepts f2, and msvc rejects…

Jamboree
- 5,139
- 2
- 16
- 36
8
votes
1 answer
Abbreviated function template vs. function template with forwarding reference param
What are the differences between
function templates with forwarding reference parameters
template
void Universal_func(T && a)
{
}
and abbreviated function templates?
void auto_fun(auto && a)
{
}
Can I replace Universal_func with…

Ajay yadav
- 4,141
- 4
- 31
- 40
8
votes
1 answer
Template argument deduction for variadic function pointer parameter - handling of ambiguous cases
Consider the following code:
#include
void f(int) { }
void f(int, short) { }
template void g(void (*)(Ts...))
{
std::cout << sizeof...(Ts) << '\n';
}
template void h(void (*)(T,…

bogdan
- 9,229
- 2
- 33
- 48
8
votes
1 answer
What is the easiest way to create a local variable with the same type as a deduced argument?
Namely:
[](auto const& foo) {
??? bar; // should be same base type as foo, minus const&
}
So far, I'm using:
typename std::remove_const::type>::type combination
But I'm really hoping theres an…

mmocny
- 8,775
- 7
- 40
- 50
7
votes
2 answers
template std::ostream& operator << (...)
Why this application doesn't compile?
#include
#include
template
std::ostream& operator << (std::ostream& out, std::array const& arr) {
for(auto& a:arr) std::cout << a << ' ';
return out;
}
int main(int…

asmbaty
- 436
- 2
- 11