Questions tagged [template-argument-deduction]

Template argument deduction is a compiler attempt to deduce template arguments when some are omitted.

692 questions
124
votes
1 answer

What are template deduction guides and when should we use them?

The C++17 standard introduces "template deduction guides". I gather they're something to do with the new template argument deduction for constructors introduced in this version of the standard, but I haven't yet seen a simple, FAQ-style explanation…
Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
87
votes
1 answer

What is a nondeduced context?

I've stumbled over "Why is the template argument deduction not working here?" recently and the answers can be summed up to "It's a nondeduced context". Specifically, the first one says it's such a thing and then redirects to the standard for…
Shoe
  • 74,840
  • 36
  • 166
  • 272
47
votes
4 answers

Make a function accepting an optional to accept a non-optional?

I'm trying to write syntactic sugar, in a monad-style, over std::optional. Please consider: template void f(std::optional) {} As is, this function cannot be called with a non-optional T1 (e.g. an int), even though there exists a…
YSC
  • 38,212
  • 9
  • 96
  • 149
47
votes
1 answer

What is the partial ordering procedure in template deduction

Reading the C++11 standard I can't fully understand the meaning of the following statement. Example are very welcome. Two sets of types are used to determine the partial ordering. For each of the templates involved there is the original function…
yuan
  • 2,434
  • 1
  • 21
  • 29
43
votes
2 answers

Template default argument loses its reference type

Consider #include #include template T foo(ARG_T v){ return std::is_reference::value; } int main() { int a = 1; std::cout << foo(a) << '\n'; std::cout <<…
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
36
votes
2 answers

Do we still need to write the empty angle brackets when using transparent std function objects?

With class template argument deduction we can write: std::less Fn; However, G++ 8.2 rejects this code: #include #include #include int main() { std::vector v= { 1, 3, 2, 7, 5, 4…
metalfox
  • 6,301
  • 1
  • 21
  • 43
30
votes
2 answers

Can C++ deduce argument type from default value?

I tried to write this function with a default template argument: template void func(int i1, int i2, A a, B b = 123){ ... } In my mind I can call it like this: func(1, 2, 3) and compiler should deduce type B as int from…
27
votes
3 answers

How can I prevent C++ guessing a second template argument?

I'm using a C++ library (strf) which, somewhere within it, has the following code: namespace strf { template inline auto range(ForwardIt begin, ForwardIt end) { /* ... */ } template inline auto…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
25
votes
3 answers

What are std::vector deduction guides in C++17?

I read about deduction guides for std::vector from using cppreference. Example: #include int main() { std::vector v = {1, 2, 3, 4}; std::vector x{v.begin(), v.end()}; // uses explicit deduction guide } So, I have some…
msc
  • 33,420
  • 29
  • 119
  • 214
25
votes
4 answers

Partial ordering with function template having undeduced context

While reading another question, i came to a problem with partial ordering, which i cut down to the following test-case template struct Const { typedef void type; }; template void f(T, typename Const::type*) { cout <<…
24
votes
4 answers

template argument deduction with strongly-typed enumerations

If I have a normal (weak) enumeration, I can use its enumerated values as non-type template parameters, like so: enum { Cat, Dog, Horse }; template bool magic(T &t) { return magical_traits::invoke(t); } and call it…
Useless
  • 64,155
  • 6
  • 88
  • 132
24
votes
3 answers

Why can't unique_ptr's template arguments be deduced?

When you have class template argument deduction available from C++17, why can't you deduce the template arguments of std::unique_ptr? For example, this gives me an error: std::unique_ptr smp(new D); That says "Argument list of class template is…
That Guy
  • 2,349
  • 2
  • 12
  • 18
23
votes
1 answer

Error with gcc but not with clang when compiling initializer list containing pointers to template function

The following snippet compiles fine in vc++ and clang++, but fails on gcc (inc 9.2) unless i add an explicit cast. Which compiler is right here? #include template void Task(void) {} int main() { for(auto p_task:…
22
votes
1 answer

How does template argument deduction work when an overloaded function is involved as an argument?

This is the more sophisticated question mentioned in How does overload resolution work when an argument is an overloaded function? Below code compiles without any problem: void foo() {} void foo(int) {} void foo(double) {} void foo(int, double)…
Leon
  • 31,443
  • 4
  • 72
  • 97
1
2 3
46 47