Questions tagged [template-argument-deduction]

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

692 questions
13
votes
2 answers

Why is template argument deduction failing for pointer-to-member-function?

With g++ 5.4, this struct B { void f() {} }; struct D : public B { void g() {} }; template void foo(void (T::*)(), void (T::*)()) {} int main() { foo(&D::f, &D::g); } fails due to "deduced conflicting types for parameter…
fizzer
  • 13,551
  • 9
  • 39
  • 61
13
votes
2 answers

Nested template argument deduction for class templates not working

In this Q&A I wrote a little wrapper class that provides reverse iterator access to a range, relying on the c++1z language feature template argument deduction for class templates (p0091r3, p0512r0) #include #include #include…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
13
votes
3 answers

Deduce template argument from std::function call signature

Consider this template function: template ReturnT foo(const std::function& fun) { return fun(); } Why isn't it possible for the compiler to deduce ReturnT from the passed call signature? bool bar() { /* ... */…
Karl von Moor
  • 8,484
  • 4
  • 40
  • 52
12
votes
1 answer

Should the order of template arguments matter when they are all deduced?

I'm getting a discrepancy between Clang, GCC and MSVC, where GCC and Clang do what I expect: #include template //< Bad //template //< OK //template…
Ben
  • 9,184
  • 1
  • 43
  • 56
12
votes
2 answers

Partial class template argument deduction in C++17

In the example below, we use the C++17 feature "Class template argument deduction" to deduce that val is of type Base: template struct Base { Base(T, U) { }; Base(T, U, V) { }; Base(V) {…
Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
12
votes
1 answer

Function template overload resolution with two parameter packs

Consider the following code: #include template int f(T...) { return 1; } template int f(T...) { return 2; } int main() { std::cout << f(1); } It compiles and prints 1 on gcc 8.2, but fails to…
user10605163
12
votes
1 answer

g++ and clang++ different behaviour with pointer to variadic template functions

Another "who's right between g++ and clang++ ?" question for C++ standard gurus. The code is the following template struct bar { }; template void foo (bar const &) { } int main () { foo(bar
max66
  • 65,235
  • 10
  • 71
  • 111
12
votes
1 answer

Are user defined deduction guides involving template template parameter as a template for guidance standard compliant

Background Yesterday I asked a question about the guarantees of deduction guides usage in case of template template parameters. I was really surprised when Barry changed his answer to the confirmation of standard complianceness of the code. My…
12
votes
2 answers

SFINAE and the address of an overloaded function

I'm experimenting with resolving the address of an overloaded function (bar) in the context of another function's parameter (foo1/foo2). struct Baz {}; int bar() { return 0; } float bar(int) { return 0.0f; } void bar(Baz *) {} void foo1(void…
Quentin
  • 62,093
  • 7
  • 131
  • 191
12
votes
5 answers

Can't deduce template type

I'm trying to pass an iterator as a template parameter to a template method, but the compiler complains that: error C2783: 'void Test::Assert(std::vector::const_iterator)': could not deduce template argument for 'T' The code that produces the…
Q-bertsuit
  • 3,223
  • 6
  • 30
  • 55
12
votes
1 answer

What's the point of "boost::mpl::identity::type" here?

I was checking the implementation of clamp in boost: template T const & clamp ( T const& val, typename boost::mpl::identity::type const & lo, typename boost::mpl::identity::type const & hi, Pred p ) …
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
12
votes
1 answer

Mixing variadic template values and variadic deduced types

Is the following perfectly defined by the standard ? #include template void f(Types&&... values) { std::cout<
Vincent
  • 57,703
  • 61
  • 205
  • 388
11
votes
2 answers

Template argument deduction for an argument of a function type

Consider the following program. #include template void f( void ( *fn )( T ) ) { fn( 42 ); } void g( int x ) { std::cout << "g( " << x << " );\n"; } int main() { f( g ); } The program compiles successfully and…
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
11
votes
2 answers

Is it possible for the template parameter to be a reference type?

I have started to learn C++ and currently I am trying to get started with templates, so please bear with me, if my wording is not 100% accurate. I am using the following literature: C++ Templates: The Complete Guide (2nd Edition) Effective Modern…
11
votes
5 answers

Array class that will accept an braced-init-list and deduce length

This has been asked before, but I'm curious to see if anything has changed in newer C++ standards. Any current or future standard is acceptable. Q: Is there anyway to create an Array class that can be initialized with a braced-init-list without…
Adam
  • 1,122
  • 9
  • 21