constexpr

`noexcept` behavior of `constexpr` functions

你。 提交于 2020-03-17 11:33:30
问题 The wording of [expr.unary.noexcept] changed in C++17 (edited following a comment by @L.F.: C++20 ) . Previously (n4140, 5.3.7 noexcept operator [expr.unary.noexcept]), my emphasis : The result of the noexcept operator is false if in a potentially-evaluated context the expression would contain (3.1) a potentially-evaluated call to a function, member function, function pointer, or member function pointer that does not have a non-throwing exception-specification ([except.spec]), unless the call

C++ constexpr : Compute a std array at compile time

对着背影说爱祢 提交于 2020-03-17 08:54:01
问题 I want to convert an "array" of bool to a integer sequence. So I need to compute an std::array at compile time. Here is my code #include <array> template<typename InputIt, typename T > inline constexpr typename std::iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value ) { typename std::iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (*first == value) { ret++; } } return ret; } template<bool ..._values> struct keep

C++ constexpr : Compute a std array at compile time

半城伤御伤魂 提交于 2020-03-17 08:52:04
问题 I want to convert an "array" of bool to a integer sequence. So I need to compute an std::array at compile time. Here is my code #include <array> template<typename InputIt, typename T > inline constexpr typename std::iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value ) { typename std::iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (*first == value) { ret++; } } return ret; } template<bool ..._values> struct keep

How to tell if `constexpr` is evaluated at compile time (without manual inspection)

被刻印的时光 ゝ 提交于 2020-03-16 08:57:20
问题 Is there a standard way to find out what the compiler does to constexpr functions? (Side note: For debug, every constexpr function is deferred to runtime by default. Why is this sensible? Is there a way to influence this?) For release it depends on the context. Obviously, for small test settings you can easily inspect the generated machine code, but this cannot be the way to go for a real project. My current 'workaround' (VC++) is to break somewhere, go to my constexpr function and (try to)

Will consteval functions allow template parameters dependent on function arguments?

孤街浪徒 提交于 2020-03-10 10:50:02
问题 In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant<int, i>::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at runtime, thus making the template instantiation impossible. In C++20 we will have consteval functions, which are required to be evaluated at compile-time, so the runtime constraint should be removed. Does it mean this code will be legal? consteval int foo(int

Will consteval functions allow template parameters dependent on function arguments?

五迷三道 提交于 2020-03-10 10:48:31
问题 In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant<int, i>::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at runtime, thus making the template instantiation impossible. In C++20 we will have consteval functions, which are required to be evaluated at compile-time, so the runtime constraint should be removed. Does it mean this code will be legal? consteval int foo(int

C++ compile time counters, revisited

房东的猫 提交于 2020-02-12 08:05:44
问题 TL;DR Before you attempt to read this whole post, know that: a solution to the presented issue has been found by myself, but I'm still eager to know if the analysis is correct; I've packaged the solution into a fameta::counter class that solves a few remaining quirks. You can find it on github; you can see it at work on godbolt. How it all started Since Filip Roséen discovered/invented, in 2015, the black magic that compile time counters are in C++, I have been mildly obsessed with the device

assert in constexpr function

回眸只為那壹抹淺笑 提交于 2020-02-04 23:53:31
问题 In trying to work out why I was getting a certain compile error, I came up with the following minimal example: constexpr void Test(bool test) { if (test) return; assert(false); } This compiles without issue with every version of clang I tried (3.7+), but fails with gcc (tested 5-8), with error: call to non-‘constexpr’ function ‘void __assert_fail(const char*, const char*, unsigned int, const char*)’ From my understanding, the function should be able to be constexpr because there is a set of

assert in constexpr function

China☆狼群 提交于 2020-02-04 23:53:29
问题 In trying to work out why I was getting a certain compile error, I came up with the following minimal example: constexpr void Test(bool test) { if (test) return; assert(false); } This compiles without issue with every version of clang I tried (3.7+), but fails with gcc (tested 5-8), with error: call to non-‘constexpr’ function ‘void __assert_fail(const char*, const char*, unsigned int, const char*)’ From my understanding, the function should be able to be constexpr because there is a set of

When can I be sure a constexpr global variable will be “forgotten”, like a C macro?

亡梦爱人 提交于 2020-02-02 12:24:26
问题 I want to use a global constexpr variable: constexpr int foo = 123; instead of a C macro: #define FOO (123) in some code I'm writing. I would like to be guaranteed the same behavior, in the sense that this will not take up space in memory at runtime, nor will it be visible/exist in the compiled object code (that is, its value will be used as an immediate where relevant). Can I get this guarantee at all? Under some conditions? Assume, of course, I'm not trying to use x's address or any such