Questions tagged [template-argument-deduction]

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

692 questions
9
votes
3 answers

Get function arguments type as tuple

Problem Given any function (or callable) type Function, how can I get its all arguments types as a tuple type ? For example, I need a trait function_traits::arguments, where: int f(); typename function_traits::arguments //…
9
votes
2 answers

Template Argument Deduction Failure and Function Parameters/Arguments Mismatch

Consider the following program: template struct A { using X = typename T::X; }; template ::X* = nullptr> void f(T, int); void f(...); template void g(T, int, typename A::X* = nullptr); // # void…
xskxzr
  • 12,442
  • 12
  • 37
  • 77
9
votes
2 answers

Using std::function with templates

So in it's most distilled form I have something like this going on, template bool f(const T &a, const T &b, std::function func) { return func(a,b); } template bool g(const T &a, const T &b) { …
Arii
  • 349
  • 2
  • 10
9
votes
1 answer

Why does std::is_same give a different result for the two types?

In the code below, why do the two ways of invoking fun: fun(num) and fun(num), give a different result when compiling? #include using namespace std; template
9
votes
1 answer

Why can't the compiler deduce auto template parameter unless I add const?

I recently had problem with code like this: constexpr auto lambda = []{}; template struct Lambda {}; template void test(Lambda) {} int main() { test(Lambda{}); } Both clang and GCC tells that it can't deduce…
9
votes
1 answer

Partial ordering of forwarding reference and normal reference with deduction guides

gcc 8.0.0 and clang 5.0.0 disagree on the behavior of this program: #include template struct A { A(const T&) { std::cout << __PRETTY_FUNCTION__ << '\n'; } A(T&&) { std::cout << __PRETTY_FUNCTION__ << '\n';…
Barry
  • 286,269
  • 29
  • 621
  • 977
9
votes
1 answer

Deduction guide and variadic templates

Consider the following code: #include #include template struct custom_wrapper { template custom_wrapper(Arg arg): data(arg) {} T data; }; template custom_wrapper(Arg arg) ->…
Vincent
  • 57,703
  • 61
  • 205
  • 388
9
votes
2 answers

Deduction guides and variadic class templates with variadic template constructors - mismatched argument pack lengths

Consider the following class definition and deduction guide: template struct foo : Ts... { template foo(Us&&... us) : Ts{us}... { } }; template foo(Us&&... us) -> foo; If I try to…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
9
votes
2 answers

Function with a size_t Template Parameter

I'm trying to understand the template function. The ultimate goal is to pass an entire array to a function. There seem to be many different ways to implement this but they all use the template function. Here's one of the simpler examples I've…
Ben Marconi
  • 161
  • 1
  • 1
  • 7
9
votes
5 answers

Template Argument Type Deduction Fails with C++11

I'm trying to understand how to use C++(11) . Here's my trivial test program #include template inline U add(typename std::enable_if::value,U>::type a, typename…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
9
votes
2 answers

The difference between int a[5] and int (&a)[5] in template parameter deduction

This question is about functions that take arrays of statically known size. Take for example the following minimal program: #include template void arrfun_a(int a[N]) { for(size_t i = 0; i < N; ++i) std::cout <<…
danielschemmel
  • 10,885
  • 1
  • 36
  • 58
8
votes
3 answers

Getting the right value_type

In my class I have a member: std::vector memory_; Now I'd like to have a fnc returning what's in the memory's first element but I do not want to specify std::string as a return type in case later I decide to use different type for this…
smallB
  • 16,662
  • 33
  • 107
  • 151
8
votes
2 answers

Deducing knowledge of original types, whilst simultaneously forwarding

Summary: I want to end up with a function that deduces the exact types it was called with and takes (e.g.) a tuple that forwards them (the types of which will be different from the exact types the function was called with). I'm stuck trying to…
Flexo
  • 87,323
  • 22
  • 191
  • 272
8
votes
1 answer

Is std::decay_t decay_copy(T&&) equivalent to auto decay_copy(auto&&)?

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3255.html defines decay_copy as follows: template std::decay_t decay_copy(T&& v) { return std::forward(v); } I just wonder: Is it equivalent to the following simpler…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
8
votes
6 answers

Default argument for a functor in a templated parameter

I would like to have a default lambda for a functor argument in my function. I am aware it is possible using a struct and operator() like this: struct AddOne { int operator()(int a) { return a+1; } }; template
Salgar
  • 7,687
  • 1
  • 25
  • 39