0

I can either write make_tuple<int,bool>(1,true) or write make_tuple(1,true) and the compiler will deduce it's types. Is this ability available for code I write or is it somehow built into the compiler to which I can't access?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Fred Finkle
  • 1,947
  • 3
  • 18
  • 21

4 Answers4

1

Template argument deduction (the proper name for this) is mandated by the standard for all function templates. The process that should be followed is explained in C++11, section 14.8.2.

Jon
  • 428,835
  • 81
  • 738
  • 806
1

Here's an example:

template <typename... Ts>
tuple<Ts...> my_make_tuple(Ts... ts)
{
    return tuple<Ts...>(ts...);
}

NOTE: This doesn't use perfect forwarding or any other tricks. It's just an example of how you can write your own function that does argument deduction.

bstamour
  • 7,746
  • 1
  • 26
  • 39
0

Compiler will be able to deduce the types provided that deduction is unambiguous. Otherwise, you may need to provide hint for compiler in form of those template arguments.

Jari
  • 2,152
  • 13
  • 20
0

The make_tuple function is just a normal function template. You could do the same thing yourself.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132