constexpr

constexpr exp, log, pow

孤者浪人 提交于 2020-01-12 06:26:10
问题 I'd like to use constexpr versions of standard <cmath> functions like exp , log , pow in a portable way. I currently have a non-portable solution g++ treats these functions as constexpr - a non-compliant extension of C++, but I am concerned about portability and future-proofing (I imagine that this extension might one day be removed from g++ ). I am interested in constexpr versions of these functions, not template metaprograms - I want the same functionality to be available both at compile

Invalid explicitly-specified argument for template parameter which is constexpr

社会主义新天地 提交于 2020-01-11 11:32:24
问题 I have a static_loop construct like this template <std::size_t n, typename F> void static_loop(F&& f) { static_assert(n <= 8 && "static loop size should <= 8"); if constexpr (n >= 8) f(std::integral_constant<size_t, n - 8>()); if constexpr (n >= 7) f(std::integral_constant<size_t, n - 7>()); if constexpr (n >= 6) f(std::integral_constant<size_t, n - 6>()); if constexpr (n >= 5) f(std::integral_constant<size_t, n - 5>()); if constexpr (n >= 4) f(std::integral_constant<size_t, n - 4>()); if

How can this code be constexpr? (std::chrono)

夙愿已清 提交于 2020-01-11 08:29:27
问题 In the standards paper P0092R1, Howard Hinnant wrote: template <class To, class Rep, class Period, class = enable_if_t<detail::is_duration<To>{}>> constexpr To floor(const duration<Rep, Period>& d) { To t = duration_cast<To>(d); if (t > d) --t; return t; } How can this code work? The problem is that operator-- on a std::chrono::duration is not a constexpr operation. It is defined as: duration& operator--(); And yet this code compiles, and gives the right answer at compile time: static_assert

constexpr overloading

[亡魂溺海] 提交于 2020-01-08 16:02:54
问题 Related: Function returning constexpr does not compile I feel like constexpr is limited in usefulness in C++11 because of the inability to define two functions that would otherwise have the same signature, but have one be constexpr and the other not constexpr. In other words, it would be very helpful if I could have, for example, a constexpr std::string constructor that takes constexpr arguments only, and a non-constexpr std::string constructor for non-constexpr arguments. Another example

reinterpret_cast with an integer literal is not a constexpr

亡梦爱人 提交于 2020-01-06 06:42:53
问题 The code below does not compile neither in gcc nor in clang . Both complain that, the reinterpret_cast to int* is not a constexpr . How can I work-around the problem? Note that I cannot modify the macro PORT , as it is defined in a 3-rd party library ( avr ). #include <iostream> #define PORT ((int *)(0x20)) constexpr int *p = PORT; // does not compile int main() { std::cout << (uintptr_t) p << "\n"; return 0; } 回答1: Put simply, if you cannot modify PORT you cannot specify PORT as constexpr .

Can consteval functions from different translation units interfere?

安稳与你 提交于 2020-01-06 05:46:07
问题 I am trying to dig into implications of a function being inline and stumbled upon this issue. Consider this small program (demo): /* ---------- main.cpp ---------- */ void other(); constexpr int get() { return 3; } int main() { std::cout << get() << std::endl; other(); } /* ---------- other.cpp ---------- */ constexpr int get() { return 4; } void other() { std::cout << get() << std::endl; } When compiled without optimizations, the program yields the following output: 3 3 Which might be not

3 different / same ways of doing N-factorial compile time in C++

淺唱寂寞╮ 提交于 2020-01-06 04:45:08
问题 I am trying to play with template metaprogramming, constexpr and if constexpr and have come up with 3 different ways of doing a N-recursive / N-factorial operation. All three examples are some I've found here on SO or by searching on the net - and then modified it, so they do the same The first example is using template metaprogramming: example 1 template<int N> struct NGenerator { static const int result = N + NGenerator<N-1>::result; }; template<> struct NGenerator<0> { static const int

May a compiler elide the evaluation of the “not-taken” branch in a constexpr function?

谁说我不能喝 提交于 2020-01-05 10:19:23
问题 While attempting to answer a question by Mehrdad, I concocted the little function below (in action at liveworkspace): template <typename T, unsigned low, unsigned high> static constexpr auto highest_index_in() -> typename std::enable_if<high >= low, unsigned>::type { return low == high ? low : high == low + 1 ? (exists<T, high>() ? high : low) : exists<T, (high + low)/2>() ? highest_index_in<T, (high+low)/2, high>() : highest_index_in<T, low, (high+low)/2>(); } // highest_index_in (where

Why is constexpr with std::forward_as_tuple not working? [duplicate]

允我心安 提交于 2020-01-05 07:11:27
问题 This question already has answers here : how to initialize a constexpr reference (3 answers) Constexpr Class taking const references not compiling (1 answer) constexpr begin of a std::array (1 answer) Closed 2 days ago . Why is the following not compiling? This is somehow counter-intuitive (not to say constexpr concepts are confusing): #include <tuple> int main() { constexpr const int a = 0; static_assert(a == 0, "Wups"); constexpr auto t2 = std::forward_as_tuple(a, a); } LIVE I assumed that

constexpr non-static member function with non-constexpr constructor (gcc,clang differ)

偶尔善良 提交于 2020-01-03 08:09:49
问题 For this code: struct S { S(int m): m(m) {} constexpr int f() const { return m; } int m; }; int main() { S s(1); } it is compiled with no warnings or errors by clang 3.6, 3.7 and 3.8 with -std=c++14 . But in g++ 5.x the following errors occur: main.cpp:4:19: error: enclosing class of constexpr non-static member function 'int S::f() const' is not a literal type constexpr int f() const { return m; } ^ main.cpp:1:8: note: 'S' is not literal because: struct S ^ main.cpp:1:8: note: 'S' is not an