Template argument deduction is a compiler attempt to deduce template arguments when some are omitted.
Questions tagged [template-argument-deduction]
692 questions
11
votes
2 answers
How to obtain pointer to reference member?
Consider this code:
struct AA
{
int& rr;
};
Is there a way to obtain pointer (or maybe reference) to AA::rr in order to obtain this?
AA* aa;
auto mm = &AA::rr; // error: cannot create pointer to reference member ‘AA::rr’
aa ->* mm;
Also in…

Vahagn
- 4,670
- 9
- 43
- 72
11
votes
2 answers
Template parameter is ambiguous: could not deduce template argument
I'm doing some kind of wrapper that looks like this:
#include
template
void Apply(void (T::*cb)(Value), T* obj, Value v)
{
(obj->*cb)(v);
}
class Foo
{
public:
void MyFunc(const int& i)
{
…

HiroshimaCC
- 486
- 4
- 11
10
votes
1 answer
g++ and clang++ different behaviour when `std::make_index_sequence` and `std::index_sequence` are used for a template parameter default type
Another "who's right between g++ and clang++?" question for C++ standard gurus.
Given the following code
#include
template >
struct foo;
template

max66
- 65,235
- 10
- 71
- 111
10
votes
1 answer
Template specialization and alias template deduction difference
I'm struggling to understand how deduction works in the following case:
template
struct AImpl
{ };
template
struct AHelper
{
using type = AImpl;
};
template

Saturnu
- 309
- 1
- 8
10
votes
1 answer
How to emulate deduction guides for template aliases?
Consider the following:
template
struct my_array
{
T values[N];
};
We can provide deduction guides for my_array, something like
template
my_array (Ts ...) -> my_array,…

lisyarus
- 15,025
- 3
- 43
- 68
10
votes
2 answers
g++ c++17 class template argument deduction not working in a very specific case
I have the following code:
template
class lit {
public:
lit(T l) : val(l) {}
T val;
};
template
class cat {
public:
cat(lit const& a, lit const& b) : a(a), b(b) {}
lit const& a;
lit const&…

Baruch
- 20,590
- 28
- 126
- 201
10
votes
1 answer
What are the deduction rules for automatic argument capture?
I saw the equivalent of this code earlier, and I was a little surprised to learn that it worked as intended:
#include
int main()
{
int a = 10;
[=]() mutable {
[&]() {
a += 10;
std::cout << "nested…

pingul
- 3,351
- 3
- 25
- 43
10
votes
1 answer
Template argument deduction/substitution failed with lambda as function pointer
I'm wondering why in the following code the compiler is unable to use lambda as the argument for function foo() (template argument deduction/substitution failed), while a simple function works:
template
void foo(int…

krojew
- 1,297
- 15
- 38
10
votes
1 answer
Should deduction guide argument initialization considered by class template specialization deduction?
As a follow up of this question, I tested the behavior of both clang and gcc. It appears that the two compiler have a different interpretation of the c++ standard.
In the example below, GCC refuses to compile, if a non copyable argument would need…

Oliv
- 17,610
- 1
- 29
- 72
10
votes
2 answers
Function template argument deduction (class vs funtion template)
Could you help me understand why the argument deduction works for the class template and does not work for the function template?
If I understand correctly, the class template defines a function, so when I call it is possible for the compiler to…

nshibalov
- 103
- 5
10
votes
1 answer
Perfect forwarding with class template argument deduction
I would like to understand how deductions guides work with universal references and std::forward, in particular to create perfectly forwarding wrappers. The code below provides a code to experiment with a functor wrapper in two cases: one with an…

Vincent
- 57,703
- 61
- 205
- 388
10
votes
2 answers
explicit call to variadic function template with empty parameter pack
Consider this simple (bad) function template for which lots of variations exist on this site:
template
R call_with(std::function f,
Args... args)
{
return f(args...);
}
And two attempts at…

Barry
- 286,269
- 29
- 621
- 977
10
votes
5 answers
template for "AnySTLContainer" c++
I am looking for a way to provide a function that takes a templated (STL) container, but requires its elements to be of a certain type (e.g. int).
These function calls should be VALID:
std::vector Argument;
void foo( Argument );
std::list…

S.H
- 875
- 2
- 11
- 27
10
votes
3 answers
Default template parameters: Why does the compiler complain about not specifying template argument?
I have this code:
struct A{};
template
struct B {
void foo() {}
};
B b; //Error: missing template arguments before 'b'
//Error: expected ';' before 'b'
//More errors
b.foo()
If I make foo() as a template function with…

badmaash
- 4,775
- 7
- 46
- 61
9
votes
7 answers
Deduce non-type template parameter
Is it possible to deduce a non-type template parameter from a template function parameter?
Consider this simple template:
template constexpr int factorial()
{
return N * factorial();
}
template <> constexpr int…

pezcode
- 5,490
- 2
- 24
- 37