constexpr

Initializing a constexpr with a const, — int vs float

你。 提交于 2019-12-01 15:39:04
I'm wondering why the integer ii is initiallized at compile time, but not the float ff here: int main() { const int i = 1; constexpr int ii = i; const float f = 1.0; constexpr float ff = f; } This is what happens when I try to compile: > g++ -std=c++11 test.cc test.cc: In function ‘int main()’: test.cc:6:24: error: the value of ‘f’ is not usable in a constant expression constexpr float ff = f; ^ test.cc:5:15: note: ‘f’ was not declared ‘constexpr’ const float f = 1.0; Constant variables of integral types with constant initializers are integral constant expressions (de facto implicitely

Static member access in constant expressions

和自甴很熟 提交于 2019-12-01 15:29:52
问题 Accessing static class member functions or variables, can be done in two ways: through an object ( obj.member_fun() or obj.member_var ) or through the class ( Class::member_fun() or Class::member_var ). However, in constexpr functions, Clang gives an error on the object access and requires to use class access: struct S { constexpr static auto s_v = 42; constexpr static auto v() { return s_v; } }; #define TEST 1 constexpr auto foo(S const& s [[maybe_unused]]) { #if TEST constexpr auto v = s.v(

How to use static_assert for constexpr function arguments in C++?

China☆狼群 提交于 2019-12-01 15:25:52
I have several brief constexpr functions in my libraries that perform some simple calculations. I use them both in run-time and compile-time contexts. I would like to perform some assertions in the body of these functions, however assert(...) is not valid in a constexpr function and static_assert(...) can not be used to check function parameters. Example: constexpr int getClamped(int mValue, int mMin, int mMax) noexcept { assert(mMin <= mMax); // does not compile! return mValue < mMin ? mMin : (mValue > mMax ? mMax : mValue); } Is there a way to check whether the function is being executed in

constexpr function with unused reference argument – gcc vs clang

▼魔方 西西 提交于 2019-12-01 15:18:11
问题 Consider the following code: template <int N, typename T> void f(T) { } template <typename T> constexpr int k(T&) { return 0; } int main() { constexpr auto i = 1; f<k(i)>([&i] { f<k(i)>(0); }); } clang++ (trunk) compiles it. g++ (trunk) fails with the following error: <source>: In lambda function: <source>:11:19: error: no matching function for call to 'f<k<const int>((* & i))>(int)' 11 | f<k(i)>(0); | ^ <source>:1:35: note: candidate: 'template<int N, class T> void f(T)' 1 | template <int N,

constexpr, static_assert, and inlining

给你一囗甜甜゛ 提交于 2019-12-01 14:53:55
问题 I previously asked about function overloading based on whether the arguments are constexpr. I'm trying to work around the disappointing answer to that question to make a smarter assert function. This is roughly what I am trying to do: inline void smart_assert (bool condition) { if (is_constexpr (condition)) static_assert (condition, "Error!!!"); else assert (condition); } Basically, the idea is that a compile-time check is always better than a run-time check if it's possible to check at

constexpr constructor with compile time validation

对着背影说爱祢 提交于 2019-12-01 10:42:46
I'd like to build up a class with the option of constexpr-ness. And, of course, I'd like to take advantage of compile time error check. Every constexpr function, constructor included, must work also at runtime, when the given parameters are not constant expression. That's should be the reason why every time you use static_assert in a constexpr function upon a function parameter it fails to compile. Said so, I've read that one can use the exception throwing mechnanism, since when the function is called upon a constant expression, those exceptions can be evaluated at compile time. If that works,

constexpr vs const vs constexpr const

天涯浪子 提交于 2019-12-01 08:11:13
const-vs-constexpr-on-variables What the guy says about constexpr is right if double is used (or float of course). However, if you change the var type from double to an integer type like int, char, etc, everything works. Why does that happen? http://ideone.com/DAWABE int main() { const int PI1 = 3; constexpr int PI2 = 3; constexpr int PI3 = PI1; // works static_assert(PI1 == 3, ""); // works const double PI1__ = 3.0; constexpr double PI2__ = 3.0; constexpr double PI3__ = PI1__; // error static_assert(PI1__ == 3.0, ""); // error return 0; } Update : the following line was a mistake, I meant PI3

What means “obey ODR” in case of inline and constexpr function?

别等时光非礼了梦想. 提交于 2019-12-01 06:43:10
I just read that constexpr and inline functions obey one-definition rule, but they definition must be identical. So I try it: inline void foo() { return; } inline void foo() { return; } int main() { foo(); }; error: redefinition of 'void foo()', and constexpr int foo() { return 1; } constexpr int foo() { return 1; } int main() { constexpr x = foo(); }; error: redefinition of 'constexpr int foo()' So what exactly means that, constexpr and inline function can obey ODR? I just read that constexpr and inline functions obey one-definition rule, but they definition must be identical. This is in

Why GCC does not evaluate constexpr at compile time?

岁酱吖の 提交于 2019-12-01 06:35:20
As an example: class something { public: static constexpr int seconds(int hour, int min, int sec) { return hour*3600+min*60+sec; } } then: printf("Look at the time: %d\n", something::seconds(10, 0, 0)); Will compile to a call to the function using g++, instead of putting a constant number. Why would g++ do that? There's no gain in it and kinda defeats the purpose of using constexpr instead of awful macros. Why would g++ do that? constexpr functions only must be evaluated at compile time in situations where the result is used as a constant expression. These include things like initializing a

why for-loop isn't a compile time expression and extended constexpr allows for-loop in a constexpr function

家住魔仙堡 提交于 2019-12-01 06:06:38
I wrote code like this #include <iostream> using namespace std; constexpr int getsum(int to){ int s = 0; for(int i = 0; i < to; i++){ s += i; } return s; } int main() { constexpr int s = getsum(10); cout << s << endl; return 0; } I understand that it works because of extended constexpr . However in this question why-isnt-a-for-loop-a-compile-time-expression , the author gave his code as follow: #include <iostream> #include <tuple> #include <utility> constexpr auto multiple_return_values() { return std::make_tuple(3, 3.14, "pi"); } template <typename T> constexpr void foo(T t) { for (auto i =