constexpr

constexpr reference to non-const object

爷,独闯天下 提交于 2019-12-05 12:12:43
Is it permitted to declare a non-const reference as constexpr ? Example code: int x = 1; constexpr int& r = x; This is accepted by gcc and clang (I tried several current and past versions of both, back to C++11, and all accepted it). However I think it should not be accepted because C++14 [dcl.constexpr/9] says: if a constexpr specifier is used in a reference declaration, every full- expression that appears in its initializer shall be a constant expression and x is not a constant expression. The language in the latest C++17 draft of [dcl.constexpr] changed and doesn't even mention constexpr

Template non-type parameter deduction

怎甘沉沦 提交于 2019-12-05 11:42:32
Is it possible to deduce template value (not type) for a c++17 function? The function foo: template<int I> int foo() { return (I); } Can be called via: foo<5>(); And will return 5. Template types can be deduced through the type of a function argument. Is it possible to do the same in some way for the template value? For example: template<int I = x> int bar(const int x) { return (I); } This obviously wont work (because for one x is required before its declaration), but might there be some C++17 trick which would allow for this? I'd like to use this as a way of setting a constant expression

Practical limitations on amount of constexpr computation

坚强是说给别人听的谎言 提交于 2019-12-05 10:58:01
As an experiment, I just put together some code to generate a std::array<uint32_t, 256> at compile time. The table contents themselves are a fairly typical CRC lookup table - about the only new thing is the use of constexpr functions to calculate the entries as opposed to putting an autogenerated magic table directly in the source code. Anyway, this exercise got me curious: would there be any practical limitations on the amount of computation a compiler would be willing to do to evaluate a constexpr function or variable definition at compile time? e.g. something like gcc's -ftemplate-depth

Constexpr decltype

走远了吗. 提交于 2019-12-05 10:55:12
I recently asked a question here ( Detecting instance method constexpr with SFINAE ) where I tried to do some constexpr detection at compile time. Eventually, I figured out that one can exploit noexcept to do this: any constant expression is also noexcept . So I put together the following machinery: template <class T> constexpr int maybe_noexcept(T && t) { return 0; } ... constexpr bool b = noexcept(maybe_noexcept(int{})); This works and b is true as you'd expect, as zero-initializing an int is a constant expression. It also correctly yields zero when it should (if I change int to some other

How can I have a temporary variable in a constexpr function?

浪尽此生 提交于 2019-12-05 10:02:41
This is a simplified version what I would like to do. constexpr float f(float a, float b){ constexpr float temp = a+b; return temp*temp*temp; } In my version, a+b is something much more complicated, so I don't want to cut and paste it three times. Using 3*(a+b) is also not a working solution for the real function. I'm trying to keep the question related to syntax, and not algebra. I can get it to work by moving a+b to it's own constexpr function, but I'd prefer to not pollute the namespace with otherwise useless functions. As you've discovered, you can't declare variables, even constexpr ones,

Why can't decomposition declarations be constexpr?

余生长醉 提交于 2019-12-05 08:27:13
问题 Consider the following snippet to test the upcoming C++17 feature decomposition declarations (formerly known as structured bindings) #include <cassert> #include <utility> constexpr auto divmod(int n, int d) { return std::make_pair(n / d, n % d); // in g++7, also just std::pair{n/d, n%d} } int main() { constexpr auto [q, r] = divmod(10, 3); static_assert(q == 3 && r ==1); } This fails on both g++7-SVN and clang-4.0-SVN with the message: decomposition declaration cannot be declared 'constexpr'

Force Clang to “perform math early” on constant values

只愿长相守 提交于 2019-12-05 07:17:03
This is related to How to force const propagation through an inline function? Clang has an integrated assembler; and it does not use the system's assembler (which is often GNU AS (GAS)). Non-Clang performed the math early, and everything "just worked". I say "early" because @n.m. objected to describing it as "math performed by the preprocessor." But the idea is the value is known at compile time, and it should be evaluated early, like when the preprocessor evaluates a #if (X % 32 == 0) . Below, Clang 3.6 is complaining about violating a constraint. It appears the constant is not being

What is the difference between a static const and constexpr variable?

夙愿已清 提交于 2019-12-05 05:32:49
I understand that a constexpr variable can be used at compiletime. For a template, or static asser for instance. But if I want to do that without constexpr I can with static const . What is since C++11/14 introduced constexpr the difference between constexpr int a = 3; //AND static const int a = 3; Thank you! Another way to see this question is which should I use? The main difference that I know is, the value of constexpr must be known in compile-time while a const static can be assigned in run-time. const static int x = rand(); 来源: https://stackoverflow.com/questions/23538440/what-is-the

C++: struct member in a switch statement

霸气de小男生 提交于 2019-12-05 03:23:16
I am writing a microprocessor emulator in C++, and one of my goals was to make it the code very readable. To implement opcodes, I have a struct which I am using to represent individual processor instructions, and it contains both the opcode and how far to advance the program counter. The idea was to group related information about each instruction. struct instruction { const int opcode; // instruction opcode const int op_size; // how far to advance Program Counter }; const instruction HALT{0x76, 1}; const instruction NOP {0x00, 1}; My original plan was to define all the opcodes using this

Difference between const and constexpr arrays

只谈情不闲聊 提交于 2019-12-05 03:08:04
Why is there a difference between const and constexpr when used with arrays? int const xs[]{1, 2, 3}; constexpr int ys[]{1, 2, 3}; int as[xs[0]]; // error. int bs[ys[0]]; // fine. I would expect both xs[0] and ys[0] to be constant expressions but only the latter is treated as such. dyp A longer comment as community wiki. The expression xs[0] is defined in [expr.sub]/1 as *((xs)+(0)) . (See below for the parantheses.) One of the expressions shall have the type “pointer to T ” and the other shall have unscoped enumeration or integral type. Therefore, the array-to-pointer conversion [conv.array]