Questions tagged [constant-expression]

Constant expressions can be evaluated at compile time.

Many languages require certain initializers to be constant expressions. For example:

  • Array bounds
  • Selectors in case statements
  • Bit-field length specification
  • Enumeration initializers
  • Non-type template arguments

A greatly restricted set of operands are allowed in constant expressions. Generally, variables are not allowed. For example, C++ allows:

  • Literals
  • Enumeration constants
  • Values declared as const that are initialized with constant expressions
  • sizeof expressions

Questions with this tag usually need help with error messages that indicate lines that require constant expressions.

215 questions
11
votes
6 answers

Example of something which is, and is not, a "Constant Expression" in C?

I'm a tad confused between what is and is not a Constant Expression in C, even after much Googleing. Could you provide an example of something which is, and which is not, a Constant Expression in C?
Dave
  • 12,408
  • 12
  • 64
  • 67
11
votes
3 answers

size() of std::array pointer in constexpr context

Let's say I have a function like: int test(std::array* data) { char buffer[data->size() * 2]; [... some code ...] } clearly the size of the buffer can be evaluated at compile time: data has a constexpr size of 8 elements, 8 * 2 = 16…
rabexc
  • 571
  • 4
  • 11
11
votes
1 answer

Can i have a negative value as constant expression in Scala?

I have an Java-Annotation that return a double value: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface DoubleValue { double value(); } When i try to attach the annotation to a field in a scala class and the value…
Klinke
  • 207
  • 1
  • 7
9
votes
2 answers

Details of what constitutes a constant expression in C?

C defines at least 3 levels of "constant expression": constant expression (unqualified) arithmetic constant expression integer constant expression 6.6 paragraph 3 reads: Constant expressions shall not contain assignment, increment, decrement,…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
9
votes
1 answer

How can I check that an expression is constant in C?

Say I have a scenario where I need to ensure that a value used in my code is a compile-time constant (e.g. perhaps a draconian interpretation of P10 rule 2 "fixed loop bounds"). How can I enforce this at the language-level in C? C supports the…
Alex Celeste
  • 12,824
  • 10
  • 46
  • 89
9
votes
2 answers

const variables may not be used in a constant expression?

Why is this C code illegal in Visual Studio 2013 Ultimate? const unsigned int x = 64; char resultBufNative[x+1]; It gives error C2057: expected constant expression. Original question I'm completely baffled by this one. Relevant function: jstring…
Pimgd
  • 5,983
  • 1
  • 30
  • 45
8
votes
2 answers

How to check if a parameter is an integral constant expression in a C preprocessor macro?

I'm currently cleaning up an existing C-library to publish it shamelessly. A preprocessor macro NPOT is used to calculate the next greater power of two for a given integral constant expression at compile time. The macro is normally used in direct…
Lutz
  • 83
  • 5
8
votes
2 answers

clang says call to void consteval function is not a constant expression

clang(trunk) gives an error for the following code: consteval void f() {} int main() { f(); // error: call to consteval function 'f' is not a constant expression // note: subobject of type 'void' is not initialized } while…
cigien
  • 57,834
  • 11
  • 73
  • 112
8
votes
2 answers

Parameterized tests in f# - This is not a valid constant expression

For some reason when passing arguments to the test via TestCase attrubute, I get the following error message about the first argument, which in this case is an array: This is not a valid constant expression or custom attribute value module…
7
votes
1 answer

Does constexpr in pointers make a difference

What is the difference between constexpr int *np = nullptr and int const *np = nullptr? np is a constant pointer to an int that is null, in both cases. Is there any specific use of constexpr in context of pointers.
7
votes
1 answer

g++ complains constexpr function is not a constant expression

I've reduced my problem to the following: struct A { static constexpr std::size_t f() { return 4; } }; template struct B : A { alignas(A::f()) char a[N]; }; I don't see what's wrong with this, yet if I try to compile using…
orlp
  • 112,504
  • 36
  • 218
  • 315
7
votes
1 answer

enum type value as the length of a array in C++

As we all know, the array length in C++ must be determined. Then we can use: const int MAX_Length=100; or: #define MAX_LENGTH 100 to determined the length of an array before compilation. But, when I read the book c++ primer by lippman, in chapter…
Kevin Wang
  • 135
  • 1
  • 8
7
votes
8 answers

C++ expected constant expression

#include #include #include #include #include using std::ifstream; using namespace std; int main (void) { int count=0; float sum=0; float maximum=-1000000; float sumOfX; float sumOfY; int size; int…
n0ob
  • 1,275
  • 8
  • 20
  • 23
7
votes
3 answers

Why does constexpr work with templates?

Consider the following code: template constexpr inline T fma(T a, T b, T c) { return a * b + c; } This compiles just fine. But why does it? In theory, constexpr functions can only call other constexpr functions. However, there is no…
Publius
  • 1,184
  • 2
  • 10
  • 27
6
votes
2 answers

Why initialization of constexpr variable from non constexpr variable succeeds in case of class

I have learnt about constexpr variables in c++ and read that int i =0; constexpr int j = i; fails because i is not a constant expression which makes sense. But then when I did the same with a variable of a class, it worked. struct C{}; int main() { …
Alan
  • 116
  • 9
1 2
3
14 15