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

Learn What auto And Auto-Typed Variables Are In Modern C++

Learn What auto And Auto Typed Variables Are In Modern C++

C++ is a highly evolved and mature programming language. The C++ language has a great set of choices of modern C++ Editor and compilers all of which come with a lot of tools, GUI components, and libraries. C++11 was a big step for functional programming and it brought many other very useful features like automatic type deduction (auto), lambda expressions, and decltype features. In this post, we explain what is auto in today’s modern C++ and how to use auto-typed variables.

What Is auto in C++?

The auto keyword arrives with the new features in C++11 and C++17 and 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.

The auto keyword was being used as an automatic data specifier (storage class specifier) until C++11. This feature was removed by the C++11 standard.

How can we use auto in modern C++?

Since C++11 the auto keyword can be used in 2 different situations,

  • As a placeholder type specifier (auto-typed variable)
  • In a function declaration

Since C++17 auto keyword can be used as,

  • Structured binding declaration

In this post we will explain auto-typed variables which comes after C++11 and available in other CLANG standards.

What are auto-typed variables in modern C++?

Auto-typed variables are a C++11 feature that allows the programmer to declare a variable of type auto, with the type itself being deduced from the variable’s initializer expression. The auto keyword is treated as a simple type specifier (that can be used with * and &), and its semantics are deduced from the initializer expression. For more information about this feature, please see the C++11 standard Proposal Document at: auto-typed variables Proposal Document.

The auto keyword is very useful, for example, it can be used as a placeholder type specifier to declare auto-typed variables.

Here is the syntax.

Is there a simple auto-typed variables example in modern C++?

Here is a simple example of how to use auto-typed variables in C++.

The auto keyword is very useful in lambda declarations as shown below.

Is there a full example of how to use auto-typed variables in modern C++?

Here are some very simple auto-typed Variables examples.

Using auto drops references, const qualifiers, and volatile qualifiers, for example,

Is there a multi-declarator auto example in C++?

The auto keyword can be used for multiple variables, and it is referred to as a ‘multi-declarator auto’. The C++11 standard includes the multi-variable form of auto declarations, such as:

The restriction with multi-declarator auto expressions is that the variables must have the same base type.

For example, the following line of code is well-formed:

because x and y have the same base type : int, while the following code:

will generate the error: [bcc64 Error] File1.cpp(11): 'auto' deduced as 'int' in declaration of 'x' and deduced as 'double' in declaration of 'y'

This feature is supported by the Clang-enhanced C++ compilers. For more information on this feature, please see the C++11 standard Proposal Document at: Multi-declarator auto

Learn What auto And Auto Typed Variables Are In Modern C++ the C++ Builder Logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. 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 cross-platform 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++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?