Questions tagged [type-deduction]

A technique to determine the datatype of a class or a variable at runtime.

304 questions
92
votes
3 answers

When does type information flow backwards in C++?

I just watched Stephan T. Lavavej talk at CppCon 2018 on "Class Template Argument Deduction", where at some point he incidentally says: In C++ type information almost never flows backwards ... I had to say "almost" because there's one or two cases,…
Massimiliano
  • 7,082
  • 2
  • 40
  • 56
57
votes
3 answers

What is the rationale behind decltype behavior?

As I understood in C++11 decltype(expression) is used to deduce the exact same type of the given expression. But when the expression is put into parentheses itself, then the deduces type is lvalue reference to the expression type. For example: int…
bobeff
  • 2,800
  • 1
  • 26
  • 51
57
votes
1 answer

Why does auto x{3} deduce an initializer_list?

I love auto in C++11. It's wonderful. But it has one inconsistency that really gets on my nerves, because I trip over it all the time: int i = 3; // i is an int with value 3 int i = int{3}; // i is an int with value 3 int i(3); // i is…
Tristan Brindle
  • 15,036
  • 2
  • 31
  • 79
57
votes
1 answer

Remove reference in decltype (return T instead of T& where T& is the decltype)

(If you're a C++11 pro, skip to the bold paragraph.) Let's say I want to write a template method which calls and returns the result of a passed object which type is the template parameter: template ReturnType doSomething(const T &…
leemes
  • 42,229
  • 18
  • 115
  • 172
33
votes
5 answers

C++ decltype deducing current function returned type

I would like to automatically deduce the returned type of the function I'm writing. Example: std::vector test(){ decltype(this_function) ret; ret.push_back(5); ret.push_back(9); return ret; } So far the best I've achieved…
Luke Givens
  • 749
  • 8
  • 15
32
votes
3 answers

Deduction of the function

Let's say we have a class template like this: template class A { public: template A(F f, Args... args) { /* Do something... */ } }; And now I want to use it in some way like this one: A a(::close,…
Serge Roussak
  • 1,302
  • 1
  • 12
  • 22
28
votes
2 answers

Are there any realistic use cases for `decltype(auto)` variables?

Both from my personal experience and from consulting answers to questions like What are some uses of decltype(auto)? I can find plenty of valuable use cases for decltype(auto) as a function return type placeholder. However, I am seriously struggling…
Vittorio Romeo
  • 82,972
  • 25
  • 221
  • 369
23
votes
2 answers

What does `new auto` do?

What does it mean when I use new auto? Consider the expression: new auto(5) What is the type of the dynamically allocated object? What is the type of the pointer it returns?
Joseph Mansfield
  • 100,738
  • 18
  • 225
  • 303
18
votes
2 answers

decltype of function parameter

Is it possible to deduce the type of a function parameter? For example, if I have: void foo(int a); I would like to deduce the type int as the type of foo's first parameter. A possible use could be: foo( static_cast< decltype(/* ??? foo's first…
Jake Cobb
  • 1,671
  • 12
  • 25
17
votes
1 answer

Template argument type deduction by conversion operator

I see example from the C++ 11 Standard (n3337, 14.8.2.3/7) struct A { template operator T***(); }; A a; const int * const * const * p1 = a; // T is deduced as int, not const int and try to reproduce it by different compilers. I changed…
user3514538
  • 2,294
  • 1
  • 11
  • 21
15
votes
3 answers

When can we omit the return type in a C++11 lambda?

As far as I know, in standard C++11 (not C++14), when omitting the return type of a lambda, its return type is deduced to be: The type of the returned expression, whenever the lambda consists of nothing but a single return statement with an…
vsoftco
  • 52,188
  • 7
  • 109
  • 221
15
votes
2 answers

Strong typedefs

Is there any way to make a complete copy of a type so that they can be distinguished in template deduction context? Take the example: #include template struct test { static int c() { static int t = 0; …
Veritas
  • 2,060
  • 2
  • 13
  • 35
14
votes
2 answers

Derive a templated class' member variable type from a template member type

The title might seem a little confusing, so here is a more thorough explanation: I have a templated class that has a vector as a member variable. The template argument is a struct (or class) that will have one certain variable. The type of this…
exocortex
  • 165
  • 6
13
votes
4 answers

Why does auto deduce this variable as double and not float?

In the snippet below, auto deduces the variable to double, but I want float. auto one = 3.5; Does it always use double for literals with a decimal point? How does it decide between float and double?
Abhinav Kinagi
  • 2,505
  • 1
  • 18
  • 35
13
votes
1 answer

Trailing class template arguments not deduced

The code below fails to compile with gcc 7.1.0, which complains about providing the wrong number of template arguments in the second line of main. This version of GCC is supposed to implement template argument deduction of class templates. I think…
tweej
  • 848
  • 4
  • 15
1
2 3
20 21