From 6d90199ea984046b522c1f2bb3acd93d953856c3 Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Sat, 1 Feb 2020 17:59:06 -0500 Subject: [PATCH] C++20 features: constexpr virtual functions, explicit(bool), char8_t. --- CPP20.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/CPP20.md b/CPP20.md index 629e1fa..1a7fdc7 100644 --- a/CPP20.md +++ b/CPP20.md @@ -11,6 +11,9 @@ C++20 includes the following new language features: - [likely and unlikely attributes](#likely-and-unlikely-attributes) - [deprecate implicit capture of this](#deprecate-implicit-capture-of-this) - [class types in non-type template parameters](#class-types-in-non-type-template-parameters) +- [constexpr virtual functions](#constexpr-virtual-functions) +- [explicit(bool)](#explicit-bool) +- [char8_t](#char8_t) C++20 includes the following new library features: - [concepts library](#concepts-library) @@ -245,6 +248,49 @@ get_foo(); // uses implicit constructor get_foo(); ``` +### constexpr virtual functions +Virtual functions can now be `constexpr` and evaluated at compile-time. `constexpr` virtual functions can override non-`constexpr` virtual functions and vice-versa. +```c++ +struct X1 { + virtual int f() const = 0; +}; + +struct X2: public X1 { + constexpr virtual int f() const { return 2; } +}; + +struct X3: public X2 { + virtual int f() const { return 3; } +}; + +struct X4: public X3 { + constexpr virtual int f() const { return 4; } +}; + +constexpr X4 x4; +x4.f(); // == 4 +``` + +### explicit(bool) +Conditionally select at compile-time whether a constructor is made explicit or not. `explicit(true)` is the same as specifying `explicit`. +```c++ +struct foo { + // Specify non-integral types (strings, floats, etc.) require explicit construction. + template + explicit(!std::is_integral_v) foo(T) {} +}; + +foo a = 123; // OK +foo b = "123"; // ERROR: explicit constructor is not a candidate (explicit specifier evaluates to true) +foo c {"123"}; // OK +``` + +### char8_t +Provides a standard type for representing UTF-8 strings. +```c++ +char8_t utf8_str[] = u8"\u0123"; +``` + ## C++20 Library Features ### Concepts library diff --git a/README.md b/README.md index 44b4fe6..e5a39f8 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ C++20 includes the following new language features: - [likely and unlikely attributes](#likely-and-unlikely-attributes) - [deprecate implicit capture of this](#deprecate-implicit-capture-of-this) - [class types in non-type template parameters](#class-types-in-non-type-template-parameters) +- [constexpr virtual functions](#constexpr-virtual-functions) +- [explicit(bool)](#explicit-bool) +- [char8_t](#char8_t) C++20 includes the following new library features: - [concepts library](#concepts-library) @@ -337,6 +340,49 @@ get_foo(); // uses implicit constructor get_foo(); ``` +### constexpr virtual functions +Virtual functions can now be `constexpr` and evaluated at compile-time. `constexpr` virtual functions can override non-`constexpr` virtual functions and vice-versa. +```c++ +struct X1 { + virtual int f() const = 0; +}; + +struct X2: public X1 { + constexpr virtual int f() const { return 2; } +}; + +struct X3: public X2 { + virtual int f() const { return 3; } +}; + +struct X4: public X3 { + constexpr virtual int f() const { return 4; } +}; + +constexpr X4 x4; +x4.f(); // == 4 +``` + +### explicit(bool) +Conditionally select at compile-time whether a constructor is made explicit or not. `explicit(true)` is the same as specifying `explicit`. +```c++ +struct foo { + // Specify non-integral types (strings, floats, etc.) require explicit construction. + template + explicit(!std::is_integral_v) foo(T) {} +}; + +foo a = 123; // OK +foo b = "123"; // ERROR: explicit constructor is not a candidate (explicit specifier evaluates to true) +foo c {"123"}; // OK +``` + +### char8_t +Provides a standard type for representing UTF-8 strings. +```c++ +char8_t utf8_str[] = u8"\u0123"; +``` + ## C++20 Library Features ### Concepts library