Template argument deduction is a compiler attempt to deduce template arguments when some are omitted.
Questions tagged [template-argument-deduction]
692 questions
7
votes
1 answer
C++ primer template universal reference and argument deduction
Hello I have this example from C++ primer:
template
void f(T&& x) // binds to nonconstant rvalues
{
std::cout << "f(T&&)\n";
}
template
void f(T const& x) // lvalues and constant revalues
{
std::cout <<…

Itachi Uchiwa
- 3,044
- 12
- 26
7
votes
1 answer
Template argument deduction when mixing variadic template with C-style variadic function
Inspired by this answer, I produced this code whose output depends on the compiler:
template
constexpr auto foo(Args&& ...args, ...) noexcept {
return sizeof...(args);
}
constexpr auto bar() noexcept {
return…

Giovanni Cerretani
- 1,693
- 1
- 16
- 30
7
votes
3 answers
Deduction failure of function call with explicit template argument list and [temp.arg.explicit]/3
[temp.arg.explicit]/3 of the C++17 standard (final draft) says about deduction of function template arguments with explicitly specified template argument lists:
In contexts where deduction is done and fails, or [...], if a template argument list is…

walnut
- 21,629
- 4
- 23
- 59
7
votes
1 answer
Template instantiation behaviour changes when using explicit namespace qualifier?
I have been experimenting with a system for composable pipelines, which involves a set of 'stages', which may be templated. Each stage handles its own setup, execution and cleanup, and template deduction is used to build a minimal list of 'state'…

Steelbadger
- 135
- 6
7
votes
3 answers
Copy templated function argument in Eigen
I am writing a generic class that utilizes Eigen data types. I already have problems assigning constructor arguments to class member variables. A simplified version of my code would be:
template
class A
{
public:
…

Thomas Auzinger
- 178
- 9
7
votes
3 answers
Why deduction guide for std::array does not allow different types?
The deduction guide for std::array requires all types be the same:
std::array arr = { 1, 2, 3.4 }; // error
What is the rationale behind such a requirement? Would there be any significant drawback if different types were allowed instead? For…

Daniel Langr
- 22,196
- 3
- 50
- 93
7
votes
3 answers
Why isn't argument deduction allowed in function return type?
The most obvious answer could be - because the standard says so.
That's fine, but I'm wrapping my head around it to understand the reasons behind this choice.
Consider the following example:
template
struct S { S(T) {} };
S f() { return…

skypjack
- 49,335
- 19
- 95
- 187
7
votes
1 answer
template argument deduction for function pointer (g++ & ICC vs Clang++ & VC++ )
Consider following program:
#include
template
void foo(const T* x) {
x();
}
void bar() { std::cout<<"bar() is called\n"; }
int main() {
foo(bar);
}
It compiles fine on clang++ & VC++ but g++ gives following compiler…

Destructor
- 14,123
- 11
- 61
- 126
7
votes
2 answers
Is template-name a deduced context?
[temp.deduct.type] paragraph 8 lists all deduced contexts, but it seems not to include template-name where template-name refers to a class template and TT refers to a template template argument. Is this a deduced context?
If it is, why?
If not,…

xskxzr
- 12,442
- 12
- 37
- 77
7
votes
1 answer
Anonymous temporaries and class template argument deduction - gcc vs clang
Consider the following code snippet:
template
struct foo
{
foo(T) { }
};
int main()
{
foo{0};
}
g++ 7 happily creates a temporary object of type foo, deducing T = int.
clang++ 5 and 6 refuse to compile the code:
error:…

Vittorio Romeo
- 90,666
- 33
- 258
- 416
7
votes
1 answer
Deduction guides for std::unordered_map in C++17
I have read about deduction guides for std::unordered_map in C++17 from using a cppreference.
Then tried to run following example, which is copied from cppreference.
#include
int main() {
// std::unordered_map m1 = {{"foo", 1},…

msc
- 33,420
- 29
- 119
- 214
7
votes
2 answers
Deduce template argument for size of initializer list
I have the following (not compilable) code:
template< size_t N >
void foo( std::array )
{
// Code, where "N" is used.
}
int main()
{
foo( { 1,2 } );
}
Here, I want to pass an arbitrary number of ints to a function foo -- for…

abraham_hilbert
- 2,221
- 1
- 13
- 30
7
votes
3 answers
How to omit perfect forwarding for deduced parameter type?
Let's say I have some function a parameter type (or several parameter types) of type which I want to be deduced. Also I want different behavior based on the fact is it rvalue or lvalue. Straightforwardly writing it leads to an obvious (for…

Predelnik
- 5,066
- 2
- 24
- 36
7
votes
1 answer
Is there a way to deduce the value of a function pointer template parameter?
C++ allows non-type template parameters to be of pointer, including function pointer, type. I recently asked a question about what this is useful for, and this is a follow up to one of the answers.
Is it posible to deduce the value of a function…

HighCommander4
- 50,428
- 24
- 122
- 194
7
votes
8 answers
Use one argument for template parameter deduction?
Let’s say I have a template function, assign(). It takes a pointer and a value and assigns the value to the pointer’s target:
template void assign(T *a, T b) { *a = b; }
int main() {
double i;
assign(&i, 2);
}
In this case I…

s4y
- 50,525
- 12
- 70
- 98