Skip to main content

Type deduction: auto and decltype



When a new variable is initialized, the compiler can figure out what the type of the variable is automatically by the initializer. For this, it suffices to use auto as the type specifier for the variable:

1
2
int foo = 0;
auto bar = foo;  // the same as: int bar = foo; 



Here, bar is declared as having an auto type; therefore, the type of bar is the type of the value used to initialize it: in this case it uses the type of foo, which is int.

Variables that are not initialized can also make use of type deduction with the decltype specifier:

1
2
int foo = 0;
decltype(foo) bar;  // the same as: int bar; 



Here, bar is declared as having the same type as foo.

auto and decltype are powerful features recently added to the language. But the type deduction features they introduce are meant to be used either when the type cannot be obtained by other means or when using it improves code readability. The two examples above were likely neither of these use cases. In fact they probably decreased readability, since, when reading the code, one has to search for the type of foo to actually know the type of bar.

Popular posts from this blog

Identifiers

A valid identifier is a sequence of one or more letters, digits, or underscore characters ( _ ). Spaces, punctuation marks, and symbols cannot be part of an identifier. In addition, identifiers shall always begin with a letter. They can also begin with an underline character ( _ ), but such identifiers are -on most cases- considered reserved for compiler-specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. In no case can they begin with a digit. C++ uses a number of keywords to identify operations and data descriptions; therefore, identifiers created by a programmer cannot match these keywords. The standard reserved keywords that cannot be used for programmer created identifiers are: alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t, char32_t, class, compl, const, constexpr, const_cast, continue, decltype, default, delete, do, double, dynamic_...

Master Excel: Unleash Pro-Level Skills with These Tips and Tricks

7 Best Free Machine Learning Resources - 2022

  Google AI ai.google Education – Google AI Learn with Google AI. Whether you're just learning to code or you're a seasoned machine learning practitioner, you'll find information and exercises in this resource center to help you develop your... Learn from ML experts at Google. Understanding Machine Learning From Theory to Algorithms The term machine learning refers to the automated detection of meaningful patterns in data. https:// cs.huji.ac.il/w~shais/Unders Machine Learning & Deep Learning Tutorials github.com GitHub - ujjwalkarn/Machine-Learning-Tutorials: machine learning and deep learning tutorials,... machine learning and deep learning tutorials, articles and other resources - GitHub - ujjwalkarn/Machine-Learning-Tutorials: machine learning and deep learning tutorials, articles and other resources This repository contains a topic-wise curated list of Machine Learning and Deep Learning tutorials, articles, and other resources. AI Weekly aiweekly.co AI ...