Search code examples
c++typesautotype-deduction

Examples of mandatory type deduction in C++


Google's C++ Style Guide at some point states:

There are several contexts in which C++ allows (or even requires) types to be deduced by the compiler.

What are some examples of mandatory type deduction?


Solution

  • A simple example is initialisation of a lambda variable. The type of the lambda is anonymous, therefore it cannot be named explicitly and must be deduced:

    auto var = [capture]{};