Access to constexpr variable inside lambda expression without capturing

Are there special rules that apply to constexpr for capturing/accessing? Yes, constexpr variables could be read without capturing in lambda: A lambda expression can read the value of a variable without capturing it if the variable has const non-volatile integral or enumeration type and has been initialized with a constant expression, or is constexpr and … Read more

What are the implications of constexpr floating-point math?

C++ imposes very few restrictions on the behavior of float and other floating-point types. This can lead to possible inconsistencies in the results, both between compilers, and between runtime/compile-time evaluation by the same compiler. Here is the tl;dr on it: At runtime In constant expressions Floating-point errors, like division by zero UB, but compilers may … Read more

New-expression with consteval constructor in constexpr context

The relevant wording is [expr.const]/13: An expression or conversion is an immediate invocation if it is a potentially-evaluated explicit or implicit invocation of an immediate function and is not in an immediate function context. An immediate invocation shall be a constant expression. Note the words ‘or conversion’ and ‘implicit invocation’ – this seems to imply … Read more

static_assert on initializer_list::size()

“Initializer lists” are just horrible kludges. Don’t: #include <initializer_list> template<typename T> void Dont(std::initializer_list<T> list) { // Bad! static_assert(list.size() == 3, “Exactly three elements are required.”); } void Test() { Dont({1,2,3}); } Do: template<typename T, std::size_t N> void Do(const T(&list)[N]) { // Good! static_assert(N == 3, “Exactly three elements are required.”); } void Test() { Do({1,2,3}); … Read more

Is it possible to use std::string in a constant expression?

As of C++20, yes, but only if the std::string is destroyed by the end of constant evaluation. So while your example will still not compile, something like this will: constexpr std::size_t n = std::string(“hello, world”).size(); However, as of C++17, you can use string_view: constexpr std::string_view sv = “hello, world”; A string_view is a string-like object … Read more

Should I declare a constant instead of writing a constexpr function?

Suppose it does something a little more complicated. constexpr int MeaningOfLife ( int a, int b ) { return a * b; } const int meaningOfLife = MeaningOfLife( 6, 7 ); Now you have something that can be evaluated down to a constant while maintaining good readability and allowing slightly more complex processing than just … Read more

Is it possible to test if a constexpr function is evaluated at compile time?

C++20 introduces is_constant_evaluated, defined in header <type_traits>, which addresses this issue. constexpr int foo(int s) { if (std::is_constant_evaluated()) // note: not “if constexpr” /* evaluated at compile time */; else /* evaluated at run time */; } Note that here the ordinary if is used instead of if constexpr. If you use if constexpr, then … Read more

Declaring defaulted assignment operator as constexpr: which compiler is right?

I think all three compilers are wrong. [dcl.fct.def.default]/3 says: An explicitly-defaulted function that is not defined as deleted may be declared constexpr or consteval only if it would have been implicitly declared as constexpr. If a function is explicitly defaulted on its first declaration, it is implicitly considered to be constexpr if the implicit declaration … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)