constexpr

Why does the false branch of “if constexpr” get compiled?

强颜欢笑 提交于 2020-08-26 10:23:26
问题 Why is this code giving error while compiling? My knowledge (and also this) of " if constexpr " says the else block shouldn't get compiled. if constexpr (true) { int a = 10; } else { int b = 10 } The error is: error: expected ‘,’ or ‘;’ before ‘}’ token Compiler used: g++ version 7.5.0 While compiling I used -std=c++17 flag. P.S. The missing ';' is intentional, just to check whether else is being compiled or not. 回答1: There are 2 separate, but related issues here. Firstly, if constexpr will

why is there a “never use non-literal type” rule in constexpr functions?

别等时光非礼了梦想. 提交于 2020-07-31 07:27:18
问题 Take the following legal code: bool bar(); template <class T> constexpr bool foo(T t) { if (t>0) { return true; } return bar(); } int main() { //constexpr bool cb1 = foo(-1); // error as expected because it would attempt to call bar() constexpr bool cb2 = foo(1); // ok } https://godbolt.org/z/UWt_3A So, as long as we don't hit a non-constexpr code-path in a compile time evaluation context our constexpr is well formed. Neat! However, if I apply the same practical notion, but happen to include

why is there a “never use non-literal type” rule in constexpr functions?

Deadly 提交于 2020-07-31 07:27:11
问题 Take the following legal code: bool bar(); template <class T> constexpr bool foo(T t) { if (t>0) { return true; } return bar(); } int main() { //constexpr bool cb1 = foo(-1); // error as expected because it would attempt to call bar() constexpr bool cb2 = foo(1); // ok } https://godbolt.org/z/UWt_3A So, as long as we don't hit a non-constexpr code-path in a compile time evaluation context our constexpr is well formed. Neat! However, if I apply the same practical notion, but happen to include

Why is reinterpret_cast not constexpr?

北慕城南 提交于 2020-07-20 07:48:10
问题 Consider the following snippet: static constexpr uint8_t a = 0; static constexpr const int8_t *b = reinterpret_cast<const int8_t *>(&a); This fails to compile with error: a reinterpret_cast is not a constant expression , because the C++ standard forbids using reinterpret_cast in constexpr . However compilation succeeds if I want to store the value b in PROGMEM (for AVR microcontrollers): static constexpr uint8_t a = 0; static const int8_t PROGMEM *const b = reinterpret_cast<const int8_t *>(&a

Will std::string end up being our compile-time string after all?

∥☆過路亽.° 提交于 2020-07-15 04:14:29
问题 Many developers and library authors have been struggling with compile-time strings for quite a few years now - as the standard (library) string, std::string , requires dynamic memory allocation, and isn't constexpr. So we have a bunch of questions and blog posts about how to get compile-time strings right: Conveniently Declaring Compile-Time Strings in C++ Concatenate compile-time strings in a template at compile time? C++ Compile-Time string manipulation (off-site) Compile-time strings with

Will std::string end up being our compile-time string after all?

丶灬走出姿态 提交于 2020-07-15 04:14:29
问题 Many developers and library authors have been struggling with compile-time strings for quite a few years now - as the standard (library) string, std::string , requires dynamic memory allocation, and isn't constexpr. So we have a bunch of questions and blog posts about how to get compile-time strings right: Conveniently Declaring Compile-Time Strings in C++ Concatenate compile-time strings in a template at compile time? C++ Compile-Time string manipulation (off-site) Compile-time strings with

Will std::string end up being our compile-time string after all?

老子叫甜甜 提交于 2020-07-15 04:14:24
问题 Many developers and library authors have been struggling with compile-time strings for quite a few years now - as the standard (library) string, std::string , requires dynamic memory allocation, and isn't constexpr. So we have a bunch of questions and blog posts about how to get compile-time strings right: Conveniently Declaring Compile-Time Strings in C++ Concatenate compile-time strings in a template at compile time? C++ Compile-Time string manipulation (off-site) Compile-time strings with

emplace_back causes link error on static constexpr member

ⅰ亾dé卋堺 提交于 2020-07-03 05:50:39
问题 Why does emplace_back take a reference of the member that requires a definition? What is the difference between emplace_back(integer literal) and emplace_back(static constexpr integer member) ? If I switch to C++17, it compiles fine. I found that in C++17 static constexpr data members are implicitly inlined. Does it mean the compiler implicitly creates a definition for them? Example code: class base { int n; public: base(int n):n(n) {} }; struct base_trait { static constexpr int n = 1; }; int

std::cout equivalent at compile time, or static_assert stringification of compile-time constant values in c++11

烂漫一生 提交于 2020-06-24 07:14:56
问题 Is there a way to print the value of a constexpr or #define d value at compile time? I want the equivalent of std::cout << , or some way to do something like constexpr int PI_INT = 4; static_assert(PI_INT == 3, const_str_join("PI_INT must be 3, not ", const_int_to_str(PI_INT))); Edit: I can do some basic compile-time printing with constexpr s, at least on gcc by doing something like template <int v> struct display_non_zero_int_value; template <> struct display_non_zero_int_value<0> { static