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
Why forwarding reference does not deduce to rvalue reference in case of rvalue?
I understand that, given an expression initializing a forwarding/universal reference,lvalues are deduced to be of type T& and rvalues of type T (and not T&&).
Thus,to allow only rvalues, one need to write
template

abir
- 1,797
- 14
- 26
6
votes
1 answer
C++ pointer-to-method template deduction doesn't compile when targeting x86, but works with x64
I've got this sample code:
struct A
{
int foo() { return 27; }
};
template
struct Gobstopper
{
};
template<>
struct Gobstopper
{
Gobstopper(int, int) { } // To differentiate from general Gobstopper…

Cameron
- 96,106
- 25
- 196
- 225
6
votes
0 answers
Declare several variables with template type auto-deduction from default parameter
In the following program struct B is parametrized by a non-type template argument with default value of a lambda object:
template
struct B {};
// ok everywhere
[[maybe_unused]] B<> b1, b2;
static_assert( std::is_same_v

Fedor
- 17,146
- 13
- 40
- 131
6
votes
1 answer
How does including gtest.h break template argument deduction for a std algorithm?
I upgraded to the latest release of Google Test, and several of my tests no longer compiled. I've reduced it to this:
#include
#include
#include
#include
#include
int main () {
const…

Adrian McCarthy
- 45,555
- 16
- 123
- 175
6
votes
1 answer
C++ primer 5th ed. function template overloading
In the book C++ Primer, there is an example about function template overloading:
// print any type we don't otherwise handle
template string debug_rep(const T &t)
{
cout << "debug_rep(T const&)\n";
ostringstream ret; // see §…

Maestro
- 2,512
- 9
- 24
6
votes
1 answer
Disambiguating an overloaded function with a given pack of parameters
I am trying to implement a function template ovl such that ovl(f) will return the overload of f taking (Foo, Bar), and very surprised with what happens with my naïve solution:
template
constexpr auto ovl(Ret…

Quentin
- 62,093
- 7
- 131
- 191
6
votes
2 answers
Template argument deduction for implicit pair
Consider the following code:
#include
#include
template
struct S {
S(std::initializer_list>) {};
};
int main()
{
S s1 {{42, 42}}; // failed due to implicit `std::pair`…

αλεχολυτ
- 4,792
- 1
- 35
- 71
6
votes
1 answer
Why is there an error "no matching function for call to 'A(A<...auto...>)'"?
In C++20, we have better NTTPs, which allows literal class types:
template
struct A{};
template // note: it is a placeholder for NTTP A
struct B{};
But when I try to specialize a template for B:
template
struct…

Twice
- 95
- 4
6
votes
1 answer
Should conversion operators be considered for function template argument deduction?
Consider the following code:
template
struct S
{
operator S();
};
template
void f(S);
int main()
{
f(S{}); // gcc ok
// clang error
}
gcc uses the conversion operator on the…

cigien
- 57,834
- 11
- 73
- 112
6
votes
2 answers
return type deduction of recursive function
Recently, I read Barry's answer to this question Recursive lambda functions in C++11:
template
struct y_combinator {
F f; // the lambda will be stored here
// a forwarding operator():
template
…

Jiashu Zou
- 148
- 5
6
votes
1 answer
Can I add a deduction guide to `std` namespace?
Suppose I want to make a new deduction guide making the following possible ?
std::string str;
std::basic_string_view sv = str;
Would that be an Ok customization ?

darune
- 10,480
- 2
- 24
- 62
6
votes
3 answers
How to use template function for implicit conversion
Very simplified example (nevermind what the class A and operators are doing, it's just for example):
#include
using namespace std;
template
class A {
public:
// implicit conversion from int
A(int a) :…

vladon
- 8,158
- 2
- 47
- 91
6
votes
3 answers
Substitution failure for template template argument
I want a helper function to instantiate a class for me. Currently it cannot compile in clang (though it compiles work in gcc), but I need it to work in clang as well. Currently I'm using clang version 6.0.0-1ubuntu2.
I'm not sure why it's failing,…

Water
- 3,245
- 3
- 28
- 58
6
votes
1 answer
Way to Only Pass Second Template Parameter
So I have a template function which has a defaulted 2nd argument. It's 1st argument can be deduced, so something like:
template
void foo(const F param)
This works fine in the general case, I'll just call foo(bar). But…

Jonathan Mee
- 37,899
- 23
- 129
- 288
6
votes
1 answer
Constructing std::function argument from lambda
I have the following templated function (C++ latest standard is enabled in the compiler - but maybe 17 would be enough).
#include
template
void MyFunction(const std::function&…

user2281723
- 519
- 1
- 5
- 16