Background: The term "syntactic sugar" was coined by Peter Landin in the 1960s. C++ has included wrapper constructs from the very beginning that simplify the writing and perception of code, without adding new capabilities compared to what can be expressed with a more verbose base syntax.
Problem: The main goal of syntactic sugar is to make the code shorter, more expressive, and readable, to reduce the likelihood of errors, and to speed up development. On the other hand, excessive complexity of the syntax can lead to confusion and hidden performance or comprehension issues in the code.
Solution: In C++, many constructs are classified as syntactic sugar that are essentially wrappers around more basic elements of the language. Examples include operator overloading, range-based for loops, initializer lists, auto, and lambda expressions.
Code example:
std::vector<int> v = {1, 2, 3, 4}; for (auto x : v) { std::cout << x << std::endl; } // Equivalent (without sugar): for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) { std::cout << *it << std::endl; }
Key features:
Does auto replace type checking at compile time, and is there any overhead when using it?
No, auto is fully deduced at compile time, with no speed loss if used correctly. Errors occur only if, through inattention, auto is not the type that was expected.
Is for (auto x : v) always the fastest way to iterate over a container?
No. This syntax may copy elements (if & is not specified), which will lead to performance loss on large objects. To avoid this, it is recommended to use a reference:
for (auto& x : v) { ... }
Does operator overloading always make the code clearer?
No! Operator overloading can be misused — if the operators are overloaded non-semantically (for instance, overloading operator+ to remove elements), the code becomes more confusing.
Using auto without considering the return type of the iterator function:
std::vector<std::pair<int, int>> data; for (auto x : data) { x.first = 0; } // Modification will not occur because of copy
Pros:
Cons:
for (auto& x : data) { x.first = 0; } // Now the modification is effective
Pros:
Cons: