C++ check if statement can be evaluated constexpr

Here’s another solution, which is more generic (applicable to any expression, without defining a separate template each time). This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20. The idea is, the overload that returns true is selected when … Read more

constexpr exp, log, pow

Have you ever watch Sprout’s implementaion? Sprout is header-only library that provide C++11/14 constexpr based Containers, Algorithms, Random numbers, Parsing, Ray tracing, Synthesizer, and others. https://github.com/bolero-MURAKAMI/Sprout/tree/master/sprout/math

Constexpr pointer value

As Luc Danton notes, your attempts are blocked by the rules in [expr.const]/2 which say that various expressions are not allowed in core constant expressions, including: — a reinterpret_cast — an operation that would have undefined behavior [Note: including […] certain pointer arithmetic […] — end note] The first bullet rules out your first example. … Read more

C++ compile time counters, revisited

After further investigation, it turns out there exists a minor modification that can be performed to the next() function, which makes the code work properly on clang++ versions above 7.0.0, but makes it stop working for all other clang++ versions. Have a look at the following code, taken from my previous solution. template <int N> … Read more

`static constexpr` function called in a constant expression is…an error?

As T.C. demonstrated with some links in a comment, the standard is not quite clear on this; a similar problem arises with trailing return types using decltype(memberfunction()). The central problem is that class members are generally not considered to be declared until after the class in which they’re declared is complete. Thus, regardless of the … Read more

Why can’t decomposition declarations be constexpr?

Question: why aren’t decomposition declarations be allowed to be constexpr? (apart from “because the Standard says so”). There is no other reason. The standard says in [dcl.dcl] p8: The decl-specifier-seq shall contain only the type-specifier auto (7.1.7.4) and cv-qualifiers. That means it can’t be declared with constexpr. This was the subject of a National Body … Read more

“constexpr if” vs “if” with optimizations – why is “constexpr” needed?

This is easy to explain through an example. Consider struct Cat { void meow() { } }; struct Dog { void bark() { } }; and template <typename T> void pet(T x) { if(std::is_same<T, Cat>{}){ x.meow(); } else if(std::is_same<T, Dog>{}){ x.bark(); } } Invoking pet(Cat{}); pet(Dog{}); will trigger a compilation error (wandbox example), because both … Read more

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