Whyever **not** declare a function to be `constexpr`?

Functions can only be declared constexpr if they obey the rules for constexpr — no dynamic casts, no memory allocation, no calls to non-constexpr functions, etc. Declaring a function in the standard library as constexpr requires that ALL implementations obey those rules. Firstly, this requires checking for each function that it can be implemented as … Read more

Why isn’t `std::initializer_list` defined as a literal type?

The standard committee seems to intend on initializer_list being a literal type. However, it doesn’t look like it’s an explicit requirement, and seems to be a bug in the standard. From ยง 3.9.10.5: A type is a literal type if it is: – a class type (Clause 9) that has all of the following properties: … Read more

Is constexpr supported with lambda functions / expressions?

Update: As of C++17, lambdas are permitted in constant expressions. Lambdas are currently (C++14) not allowed in constant expressions as per [expr.const]/(2.6), but they will once N4487 is accepted (which can be found in the working draft N4582): This proposal suggests allowing lambda-expressions in constant expressions, removing an existing restriction. The authors propose that certain … Read more

When does a constexpr function get evaluated at compile time?

constexpr functions will be evaluated at compile time when all its arguments are constant expressions and the result is used in a constant expression as well. A constant expression could be a literal (like 42), a non-type template argument (like N in template<class T, size_t N> class array;), an enum element declaration (like Blue in … Read more

enum vs constexpr for actual static constants inside classes

For the record, the static constexpr version will work like you’d expected in C++17. From N4618 Annex D.1 [depr.static_constexpr]: D.1 Redeclaration of static constexpr data members [depr.static_constexpr] For compatibility with prior C++ International Standards, a constexpr static data member may be redundantly redeclared outside the class with no initializer. This usage is deprecated. [Example: struct … Read more

What’s the difference between static constexpr and static inline variables in C++17?

You don’t have to specify an initializer for mySecondVar at the point of declaration. Nor is the initializer required to be constexpr itself. This means that if we attempt to define myFirstVar like this: class MyClass { static constexpr int myFirstVar; }; int MyClass::myFirstVar = 1; Or like this: #include <cstdlib> class MyClass { static … Read more

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