Introduction
What is C++? My definition is that C++ is a set of progressive standards. So far, there are C++98/03 (small modification), C++ 11 (Major Release), C++ 14, C++17, C++20, C++23. Each standard introduced new features, and remove some un-necessary features. Some examples are:
type_traits
: introduced in C++11 to help with generic programming- Lambda expression was introduced in C++11. But it was expanded and modified in all later C++ releases.
auto_ptr
was deprecated in C++11 and removed in C++17. Its successor,unique_ptr
andshared_ptr
are much more widely used now.
Each standard is a file that could be purchased in this link, and C++ implementation is done by a few vendors, such as Clang, GCC, MSVC. Compilers however, may not be consistent to the current standard, especially if they are relatively early in their version. For the below snippet, on Compiler Explorer, compiler x86-64 gcc 14.2
would fail the compilation here, because we need constexpr int i
instead of int i
. In contrast, compiler x86-64 clang 10.1
could compile fine, but its assembly code shows that foo() will be evaluated during run time, (which is an inconsistency with the standard)
1
2
3
4
5
consteval void foo(int i){ return i+1;}
int main(){
int i = 1;
foo(i);
}
C++14 Features
C++ 17 Features
- SIMD and Vectorization in
std::for_each
- std::optional to indicate whether a variable has trustable value
- std::reduce to aggregates (or “reduce”) a container into something else
- Structural binding to make unpacking easier
- PMR containers
- if constexpr
- if with initializer
- Insert_or_assign
Cpp 20 Features
C++20 is a large coding standard upgrade (from C++ 17 code) with lots of new paradigms.
- Template lambda for more explicit typing See here
- Concepts for type constraints, see here
- Ranges For Lazy Evaluation
- Constexpr on dynamically allocated objects
- Coroutine For Async Programming
- Ranges
Summary
Feature | C++17 | C++20 | Benefit |
---|---|---|---|
Template Lambdas | auto only |
template<> |
More type flexibility |
Concepts (std::integral ) |
enable_if |
Cleaner syntax | Faster compilation |
Ranges (views::filter ) |
Manual loops | Functional style | Concise, lazy evaluation |
Coroutines (co_await ) |
Threads + Futures | Native coroutines | Simpler async code |
constexpr STL Containers |
Limited | std::vector<> allowed |
Compile-time optimizations |