C++C++17C++20Learn C++

What Are The New Rules For Auto Deduction In C++ 17?

What Are The New Rules For Auto Deduction In C++ 17

The features of C++17 were a big step in the history of C++ and brought a lot of new features. In C++17, there are new auto rules for direct-list-initialization. This feature in C++, changes the rules for auto deduction from a braced-init-list. In this post, we explain what these new rules for auto deduction with examples are.

What is auto keyword in C++?

The auto keyword arrives with the new features in C++11 and improved in C++17, can be used as a placeholder type specifier (an auto-typed variable), or it can be used in function declaration, or in a structured binding declaration. The auto keyword can be used with other new CLANG standards like C++14, C++17, etc.

What are the new rules for auto deduction in C++ 17?

In C++17, For copy-list-initialization, the auto deduction will either deduce a std::initializer_list (if the types of entries in the braced-init-list are all identical) or be ill-formed otherwise.

Note that, auto a = {1, 2, 3}, b = {1}; remains unchanged and deduces initializer_list<int>. This change is intended as a defect resolution against C++14.

Now, let’s see the examples below.

Auto deduction from braced-init-list Rule #1

For direct list-initialization: For a braced-init-list with only a single element, the auto deduction will deduce from that entry. In the example below, there is a single member in the braced-init-list, and this is automatically defined as an initializer_list that consists of int members.

Auto deduction from braced-init-list Rule #2

For direct list-initialization: For copy-list-initialization, auto deduction will either deduce a std::initializer_list (if the types of entries in the braced-init-list are all identical) or be ill-formed otherwise. For a braced-init list with more than one element, the auto deduction will be ill-formed. Here is an example of creating an initilizer_list with an auto keyword,

Here above, a is automatically defined as an initializer_list that consists with int members, std::initializer_list<int>.

Here above, aa is automatically defined as an initializer_list that consists with float members, std::initializer_list<float>.

Example below, gives error, because there are multiple element types (int and float), according to above “types of entries in the braced-init-list are all should be identical”

Auto deduction from braced-init-list Rule #3

For copy-list-initialization, auto deduction will either deduce a std::initializer_list. If the initialization is direct-list-initialization then the braced-init-list shall contain only a single initializer-clause L.

Before the C++17, auto a{1, 2, 3}, b{1}; was allowed and both variables were of type initializer_list<int>. Now auto a{1, 2, 3}; is ill-formed and auto b{1}; declares an int.

In this example below, c is declared as an int type,

in this concept, we can only list a single member, here example below gives error,

Is there a full example about auto deduction from braced-init-list in C++ 17?

Here is a full example about auto deduction from braced-init-list in C++17,

For more details, please see this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3922.html

What Are The New Overloads For Ranges In C++ 14 C++ Builder logo

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?