Keyword auto in C++ ( type inference or type deduction ) | Programming in C++

Keyword auto in C++ ( type inference or type deduction ) | Programming in C++

Keyword auto in C++ is explained in few simple steps:

The keyword auto in C++ (Type Inference) refers to automatic deduction of the data type. Deduction happens at compile time.

auto in C++

In C++11, the meaning of the auto keyword has changed, consider the following statement:

  double d = 5.0;

Do you know what is type inference (or type deduction) in C++ 11?

Starting with C++11, When initializing a variable, the auto keyword can be used in place of the variable type to tell the compiler to infer the variable’s type from the initializer’s type.

For example:

auto d = 5.0; // 5.0 is a double literal, so d will be type double
auto i = 1 + 2; // 1 + 2 evaluates to an integer, so 'i' will be type int

auto type deduction even works with the return values from functions:

int add(int x, int y)
{
    return x + y;
}

int main()
{
    auto sum = add(5, 6); // add() returns an int, so sum will be          type int
    return 0;
}

Trailing return type syntax in C++11 :

C++11 also added the ability to use a trailing return syntax, where the return type is specified after the rest of the function prototype.

Consider the following function declaration:

int add(int x, int y);

In C++11, with auto this could be equivalently written as:

auto add(int x, int y) -> int;

Type inference for functions in C++14 :

In C++14, the auto keyword was extended to be able to auto-deduce a function’s return type.

Consider:

auto d = 5.0; // 5.0 is a double literal, so d will be type double
auto i = 1 + 2; // 1 + 2 evaluates to an integer, so 'i' will be type int

Points to remember about auto keyword in C++ :

These points will help you in understanding these concepts in details and
help you in using auto keyword in real-time.

1. Const value to non-const value except reference and pointer.

2. Array becomes pointer and array to reference becomes reference-to-an array

3. Data members can’t be auto. Only int, but it should be static const.

4. Function parameter and return type can’t be auto

Example:

auto a=10, b=20;
auto i=1, *ptr = &a, &ref = b;
auto j=10, str = “error”; // compile error

Explained each point with an example:

const and volatile are removed:

Example:

const vector values;
auto a = values; // const remove- auto deduce to int

Remain const in case of reference and pointer:

Example:

const vector values;
auto a = values; // const removed
auto &b = values; // remain const
auto *ptrc = &values; //remain const

Arrays are turned into pointers:

Example:

Y arr[20];
auto value = arr; // Y* - pointer
auto &value1 = arr; // reference to an array

The data member of the class can’t be auto:

Example:

class X {
public:
auto i = 1; //compile error
}

Function can’t accept parameters as auto:

Example:

void fun1(auto a, auto b) // compile error

The return value of a function can be auto Cpp14:

Example:

auto fun1(){ return 10; }

Read more …….

2 thoughts on “Keyword auto in C++ ( type inference or type deduction ) | Programming in C++

Leave a Reply