constexpr

How to obtain constexpr `.size()` of a non-static std::array member

社会主义新天地 提交于 2021-02-11 12:41:55
问题 Given that std::array<T,N>::size is constexpr, in the snippet below Why does it matter that Foo1::u is not a static member? The type is known at compile time and so is its size() . What's wrong with Foo2::bigger() ? Listing: // x86-64 gcc 10.1 // -O3 --std=c++20 -pedantic -Wall -Werror #include <array> #include <cstdint> union MyUnion { std::array<uint8_t,32> bytes; std::array<uint32_t,8> words; }; struct Foo1 { MyUnion u; static constexpr size_t length {u.bytes.size()}; //invalid use of non

constexpr literal initialization with a constexpr

会有一股神秘感。 提交于 2021-02-10 06:15:17
问题 simple example here: static constexpr const char literal1[] = "abcde"; static constexpr const char literal2[] = literal1; compilation error. How to make it work and why it doesn't? 回答1: update: In response to comment, here's a revised verson. The class immutable::string models a constexpr string-like object which tracks the original string literal. It's very similar to c++17's string_view except that the template constructor avoids the need for a call to strlen at any time. #include <cstdint>

Constexpr Factorial Compilation Results in VS2015 and GCC 5.4.0

感情迁移 提交于 2021-02-10 05:58:45
问题 Wondering if the following surprises anyone, as it did me? Alex Allain's article here on using constexpr shows the following factorial example: constexpr factorial (int n) { return n > 0 ? n * factorial( n - 1 ) : 1; } And states: Now you can use factorial(2) and when the compiler sees it, it can optimize away the call and make the calculation entirely at compile time. I tried this in VS2015 in Release mode with full optimizations on (/Ox) and stepped through the code in the debugger viewing

Are constexpr evaluated on target platform?

风流意气都作罢 提交于 2021-02-09 12:13:24
问题 I wonder if, for example, evaluated compiled on a little endian platform will return true on a big endian target platform. constexpr bool is_little_endian() { int num = 1; return (1 == *(char *)&num); } In other words, are constexpr evaluated as if on the target? EDIT: This example isn't correct, but the question is still active. 回答1: First off: If you compile code for a given target, then the compiler will generate code for that target. This, of course, includes expressions that are

Why references can't be used with compile time functions?

左心房为你撑大大i 提交于 2021-02-08 05:30:10
问题 I have two snippets. The first snippet: #include <string> template <typename T> constexpr bool foo(T&&) { return false; } int main() { std::string a; if constexpr (foo(a)) { } } The second snippet: #include <string> template <typename T> constexpr bool foo(T&&) { return false; } int main() { std::string a; std::string& x = a; if constexpr (foo(x)) { } } The first one compiles, but the second one does not compile (error message: error: the value of ‘x’ is not usable in a constant expression .

Call non constexpr from constexpr template function

与世无争的帅哥 提交于 2021-02-07 13:35:00
问题 I stumbled on constexpr template functions calling non constexpr functions: In the following snippet bar fails to compile as expected due to the call of non constexpr set but foo compiles. Can anyone tell me the reason why foo compiles? template<class T> void set(T& x){ x++; } template<class T> constexpr void foo(T& x){ set<T>(x); } constexpr void bar(int& x){ set<int>(x); } void bar(){ int x = 5; foo(x); bar(x); } The compiler fails to compile with the error: <source>: In function 'constexpr

Call non constexpr from constexpr template function

落花浮王杯 提交于 2021-02-07 13:33:27
问题 I stumbled on constexpr template functions calling non constexpr functions: In the following snippet bar fails to compile as expected due to the call of non constexpr set but foo compiles. Can anyone tell me the reason why foo compiles? template<class T> void set(T& x){ x++; } template<class T> constexpr void foo(T& x){ set<T>(x); } constexpr void bar(int& x){ set<int>(x); } void bar(){ int x = 5; foo(x); bar(x); } The compiler fails to compile with the error: <source>: In function 'constexpr

Should decltype(foo(1)) instantiate the constexpr function template foo?

…衆ロ難τιáo~ 提交于 2021-02-07 12:04:49
问题 The following code compiles with with gcc and MSVC, but fails using clang I tested with clang-3.5 and current trunk). template <typename T> constexpr auto wrong = false; template <typename T> constexpr auto foo(const T t) -> int { static_assert(wrong<T>, ""); return {}; } using F = decltype(foo(1)); int main() {} clang instantiates the function body and stumbles over the static_assert . gcc and MSVC just look at the function declaration and ignore the static_assert in the body. If you remove

C++11 initialize array with uniform value in constexpr function

时光毁灭记忆、已成空白 提交于 2021-02-07 06:45:07
问题 I have a class template which builds a simple array based on the template parameters as one of its members. I need to be able to initialize every element in the array to a single value in one of the constructors. Unfortunately this constructor must be constexpr . The relevant part boils down to: template <typename T, size_t N> class foo { T data[N]; constexpr foo(T val) { // initialize data with N copies of val } }; Using std::fill or a loop is incompatible with the constexpr requirement.

Why compile-time floating point calculations might not have the same results as run-time calculations?

安稳与你 提交于 2021-02-07 06:45:06
问题 In constexpr: Introduction, the speaker mentioned "Compile-time floating point calculations might not have the same results as runtime calculations": And the reason is related to "cross-compiling". Honestly, I can't get the idea clearly. IMHO, different platforms may also have different implementation of integers. Why does it only affect floating points? Or I miss something? 回答1: You're absolutely right that, at some level, the problem of calculating floating-point values at compile time is