C++

C++ - Cpp14, 17, 20 Features

Lambda Capture

Posted by Rico's Nerd Cluster on February 1, 2023

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 and shared_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

Cpp 20 Features

C++20 is a large coding standard upgrade (from C++ 17 code) with lots of new paradigms.

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