diff --git a/CPP20.md b/CPP20.md index 00590bb..78c6cfd 100644 --- a/CPP20.md +++ b/CPP20.md @@ -23,6 +23,7 @@ C++20 includes the following new library features: - [std::span](#stdspan) - [bit operations](#bit-operations) - [math constants](#math-constants) +- [std::is_constant_evaluated](#stdis_constant_evaluated) ## C++20 Language Features @@ -426,6 +427,17 @@ std::numbers::pi; // 3.14159... std::numbers::e; // 2.71828... ``` +### std::is_constant_evaluated +Predicate function which is truthy when it is called in a compile-time context. +```c++ +constexpr bool is_compile_time() { + return std::is_constant_evaluated(); +} + +constexpr bool a = is_compile_time(); // true +bool b = is_compile_time(); // false +``` + ## Acknowledgements * [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features. * [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics. diff --git a/README.md b/README.md index adef75b..0be19de 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ C++20 includes the following new library features: - [std::span](#stdspan) - [bit operations](#bit-operations) - [math constants](#math-constants) +- [std::is_constant_evaluated](#stdis_constant_evaluated) C++17 includes the following new language features: - [template argument deduction for class templates](#template-argument-deduction-for-class-templates) @@ -518,6 +519,17 @@ std::numbers::pi; // 3.14159... std::numbers::e; // 2.71828... ``` +### std::is_constant_evaluated +Predicate function which is truthy when it is called in a compile-time context. +```c++ +constexpr bool is_compile_time() { + return std::is_constant_evaluated(); +} + +constexpr bool a = is_compile_time(); // true +bool b = is_compile_time(); // false +``` + ## C++17 Language Features ### Template argument deduction for class templates