constexpr

Why doesn't an if constexpr make this core constant expression error disappear?

陌路散爱 提交于 2019-11-26 19:57:41
问题 In reference to this question. The core constant expression that is used to initialize the constexpr variable y is ill-formed. So much is a given. But if I try to turn the if into an if constexpr : template <typename T> void foo() { constexpr int x = -1; if constexpr (x >= 0){ constexpr int y = 1 << x; } } int main(){ foo<int>(); } The error persists. With GCC 7.2 still giving: error: right operand of shift expression '(1 << -1)' is negative [-fpermissive] But I thought that the semantic

constexpr vs. static const: Which one to prefer?

女生的网名这么多〃 提交于 2019-11-26 19:56:35
问题 For defining compile-time constants of integral types like the following (at function and class scope), which syntax is best? static const int kMagic = 64; // (1) constexpr int kMagic = 64; // (2) (1) works also for C++98/03 compilers, instead (2) requires at least C++11. Are there any other differences between the two? Should one or the other be preferred in modern C++ code, and why? EDIT I tried this sample code with Godbolt's CE: int main() { #define USE_STATIC_CONST #ifdef USE_STATIC

static_assert on initializer_list::size()

ぃ、小莉子 提交于 2019-11-26 19:35:39
问题 Why is std::initializer_list<_E>::size not allowable in a static_assert, even though it's declared as a constexpr in my libstdc++ (v. 4.6)? For example, the following code: template<class T, int Length> class Point { public: Point(std::initializer_list<T> init) { static_assert(init.size() == Length, "Wrong number of dimensions"); } }; int main() { Point<int, 3> q({1,2,3}); return 0; } gives the following error: test.C: In constructor ‘Point<T, Length>::Point(std::initializer_list<_Tp>) [with

What does it mean to “poison a function” in C++?

走远了吗. 提交于 2019-11-26 19:00:17
问题 At the very end of Scott Schurr's talk "Introducing constexpr" at CppCon, he asks "Is there a way to poison a function"? He then explains that this can be done (albeit in a non-standard way) by: Putting a throw in a constexpr function Declaring an unresolved extern const char* Referencing the unresolved extern in the throw I sense that I'm a bit out of my depth here, but I'm curious: What does it mean to "poison a function"? What is the significance/usefulness of the technique he outlines?

C++11 - static_assert within constexpr function?

故事扮演 提交于 2019-11-26 18:48:07
How would one properly do a static_assert within a constexpr function? For example: constexpr int do_something(int x) { static_assert(x > 0, "x must be > 0"); return x + 5; } This is not valid C++11 code, because a constexpr function must only contain a return statement. I don't think that the standard has an exception to this, although, the GCC 4.7 does not let me compile this code. This is not valid C++11 code, because a constexpr function must only contain a return statement. This is incorrect. static_assert in a constexpr function are fine. What is not fine is using function parameters in

Lookup table with constexpr

这一生的挚爱 提交于 2019-11-26 18:47:36
I'm looking to create a lookup table of coordinates, something like: int a[n][2] = {{0,1},{2,3}, ... } For a given n , to be created at compile time. I started looking into constexpr , but is seems like a function returning a constexpr std::vector<std::array <int, 2> > isn't an option, as I get: invalid return type 'std::vector<std::array<int, 2ul> >' of constexpr function How can create such a compile time array? dyp I'll dump the code first, adding references and comments where necessary/appropriate later. Just leave a comment if the result is somewhat close to what you're looking for.

Is constexpr really needed?

戏子无情 提交于 2019-11-26 18:26:30
问题 I have been looking at the new constexpr feature of C++ and I do not fully understand the need for it. For example, the following code: constexpr int MaxSize() { ... return ...; } void foo() { int vec[MaxSize()]; } can be replaced by: int MaxSize() { ... return ...; } static const int s_maxSize = MaxSize(); foo() { int vec[s_maxSize]; } Update The second example is actually not standard ISO C++ (thanks to several users for pointing this out) but certain compilers (e.g. gcc) support it. So it

enum vs constexpr for actual static constants inside classes

女生的网名这么多〃 提交于 2019-11-26 17:59:12
问题 Let me start by stating my intent. In the olden (C++) days, we would have code like: class C { public: enum {SOME_VALUE=27}; }; Then we could use SOME_VALUE throughout our code as a compile time constant and wherever the compiler would see C::SOME_VALUE , it would just insert the literal 27. Now days, it is seems more acceptable to change that code to something like: class C { public: static constexpr int SOME_VALUE=27; }; This looks much cleaner, gives SOME_VALUE a well defined type and

Is constexpr supported with lambda functions / expressions?

独自空忆成欢 提交于 2019-11-26 17:53:54
struct Test { static const int value = []() -> int { return 0; } (); }; With gcc-4.6 I get something like, error: function needs to be constexpr . I have tried multiple combinations of putting constexpr at various places, but no luck. Is constexpr supported for lambda functions as well (irrespective of return type specified or not) ? What is the correct syntax ? Any work around possible ? Update : As of C++17, lambdas are permitted in constant expressions. Lambdas are currently (C++14) not allowed in constant expressions as per [expr.const]/(2.6), but they will once N4487 is accepted (which

What am I allowed to do with a static, constexpr, in-class initialized data member?

本秂侑毒 提交于 2019-11-26 17:51:59
问题 This is probably a bit of an unusual question, in that it asks for a fuller explanation of a short answer given to another question and of some aspects of the C++11 Standard related to it. For ease of reference, I shall sum up the referenced question here. The OP defines a class: struct Account { static constexpr int period = 30; void foo(const int &) { } void bar() { foo(period); } //no error? }; and is wondering why he gets no error about his usage of an in-class initialized static data