constexpr

How to generate a constexpr version string from three integers (or perhaps a git/SVN commit/rev. string)?

我们两清 提交于 2019-11-28 20:49:44
Say I have constexpr const std::uint8_t major = 1; constexpr const std::uint8_t minor = 10; constexpr const std::uint8_t bugfix = 0; and I want constexpr const char* version_string(){ ... } to return the equivalent of "1.10.0" in this example, how would I do it? I assume I'll need both of these, in constexpr : integer to string conversion string concatenation The problem is purely academic, and I see little to no use to actually have it constexpr other than "it's possible". I just can't see how this would pan out. I'm willing to accept C++1y solutions that work on GCC 4.9 and Clang 3.4/3.5. I

What is Allowed in a constexpr Function?

放肆的年华 提交于 2019-11-28 19:43:09
constexpr functions are not supposed to contain: A definition of a variable of non-literal type But in this answer a lambda is defined in one: https://stackoverflow.com/a/41616651/2642059 template <typename T> constexpr auto make_div(const T quot, const T rem) { return [&]() { decltype(std::div(quot, rem)) result; result.quot = quot; result.rem = rem; return result; }(); } and in my comment I define a div_t in one: How can I Initialize a div_t Object? template <typename T> constexpr decltype(div(T{}, T{})) make_div(const T quot, const T rem) { decltype(div(T{}, T{})) x{}; x.quot = quot; x.rem

Whyever **not** declare a function to be `constexpr`?

一个人想着一个人 提交于 2019-11-28 19:28:29
问题 Any function that consists of a return statement only could be declared constexpr and thus will allow to be evaluated at compile time if all arguments are constexpr and only constexpr functions are called in its body. Is there any reason not to declare any such function constexpr ? Example: constexpr int sum(int x, int y) { return x + y; } constexpr i = 10; static_assert(sum(i, 13) == 23, "sum correct"); Could anyone provide an example where declaring a function constexpr would do any harm?

When and why would you use static with constexpr?

寵の児 提交于 2019-11-28 18:47:21
As a disclaimer, I have done my research on this before asking. I found a similar SO question but the answer there feels a bit "strawman" and didn't really answer the question for me personally. I've also referred to my handy cppreference page but that doesn't offer a very "dumbed down" explanation of things most times. Basically I'm still ramping up on constexpr , but at the moment my understanding is that it requires expressions to be evaluated at compile time. Since they may only exist at compile time, they won't really have a memory address at runtime. So when I see people using static

static constexpr variable vs function

不羁岁月 提交于 2019-11-28 17:10:23
Is there a difference between declaring floating point constant as a static constexpr variable and a function as in example below, or is it just a matter of style? class MY_PI { public: static constexpr float MY_PI_VAR = 3.14f; static constexpr float MY_PI_FUN() { return 3.14f; } } Morwenn constexpr functions Functions have an advantage that free variables do not have (until C++14 that is): they can easily be templated without some class boilerplate. That means you can have your pi with a precision depending on a template argument: template<typename T> constexpr T pi(); template<> constexpr

Relation between constexpr and pure functions

这一生的挚爱 提交于 2019-11-28 13:16:53
Am I right, that: Any function defined with constexpr is a pure function , and Any pure function can be and must be defined with constexpr if it's not very expensive for compiler. And if so, why arent <cmath> 's functions defined with constexpr ? To add to what others have said, consider the following constexpr function template: template <typename T> constexpr T add(T x, T y) { return x + y; } This constexpr function template is usable in a constant expression in some cases (e.g., where T is int ) but not in others (e.g., where T is a class type with an operator+ overload that is not declared

Lambda capturing constexpr object

杀马特。学长 韩版系。学妹 提交于 2019-11-28 13:16:15
GCC 4.7.2 compiles this: constexpr int i = 5; []{ std::integral_constant< int, i >(); }; // nonstandard: i not captured but not this: constexpr int i = 5; [&i]{ std::integral_constant< int, i >(); }; // GCC says i not constexpr The latter example appears correct to me, according to C++11 §5.1.2/15: An entity is captured by reference if it is implicitly or explicitly captured but not captured by copy. It is unspecified whether additional unnamed non-static data members are declared in the closure type for entities captured by reference. It seems the captured object i inside the lambda refers to

constexpr: definition and declaration for constexpr members

☆樱花仙子☆ 提交于 2019-11-28 11:47:26
问题 If I want to use some convenience stuff like make_array I have no chance to declare my array first and later make the definition as done in "earlier" times because the type of my var is not available before definition. So I found this answer: Undefined reference to static constexpr char[] In the following example I wrote this solution which compiles fine with gcc and I am not sure that this is really valid c++ code, because it is more or less a declaration with definition and later a

`static constexpr` function called in a constant expression is…an error?

梦想与她 提交于 2019-11-28 10:43:57
I have the following code: class MyClass { static constexpr bool foo() { return true; } void bar() noexcept(foo()) { } }; I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this would be perfectly acceptable. However, g++ gives me the following error: error: ‘static constexpr bool MyClass::foo()’ called in a constant expression This is...less than helpful, since the ability to call a function in a constant expression is the entire point of constexpr . clang++ is a little more helpful. In addition to an error message stating that the

constexpr error at compile-time, but no overhead at run-time

送分小仙女□ 提交于 2019-11-28 10:43:15
There is a well-known trick to cause a compile-time error in the evaluation of a constexpr function by doing something like this: constexpr int f(int x) { return (x != 0) ? x : throw std::logic_error("Oh no!"); } And if the function is used in a constexpr context you will get a compile-time error if x == 0 . If the argument to f is not constexpr , however, then it will throw an exception at run time if x == 0 , which may not always be desired for performance reasons. Similar to the theory of assert being guarded by NDEBUG , is there a way to cause a compile-time error with a constexpr function