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
0
votes
2 answers

How can I set a working breakpoint to a constant expression?

I have Perl code that uses a constant with an initializing block like this: use constant C => map { ...; } (0..255); When I try to set a breakpoint at the ...; line, it does not work, meaning: I can set the breakpoint, but the debugger does not…
U. Windl
  • 3,480
  • 26
  • 54
0
votes
0 answers

Receiving "x cannot appear in constant-expression" and makefile

I'm using a templatized class with data member int arr[], and my templatized value is a size_t to allow me to create an array of variable size. When I try to create an object of my class, I receive an error stating "Error: n1 cannot appear in…
0
votes
1 answer

Preprocessor constant folding

I have a fundamental question regarding the C preprocessor constant evaluation and I would like some help in understanding if the preprocessor helps in optimizing the code in a situation like this. I understand that the preprocessor merely "replaces…
0
votes
1 answer

What is the logic behind the bitwise not operator on constant integer values?

This question may sound silly but I can not understand the idea behind the not operator on constant integers. I get the following results: not $FF => $FF00 not $FFFF => $FFFF0000 not $FFFFFFFF => $00 not $FFFFFFFFFFFFFFFF => $00 The first two…
ventiseis
  • 3,029
  • 11
  • 32
  • 49
0
votes
1 answer

How to build code that contains constant expression with c++11

Hello I am testing new c++ library : TagParser from Martchus https://github.com/Martchus/tagparser I am getting that error when I compile the following code: CODE: #include #include using…
user9191494
0
votes
1 answer

How to get the name and value of ContstantExpression in x => x.property

I'm creating a simple way to get the Name and Value of Expressions in C#. However, I found a case I cannot figure out. See the code below: public void GetValue_Object_TIn_Property_Test() { string val = "value"; var tuple = Tuple.Create(new…
van Nijnatten
  • 404
  • 5
  • 15
0
votes
1 answer

Enum value dependant function call

Here is simplified setup of my application: class Engine { void run(); { // main program loop while (state != gameState::quit) step(); // ERROR } template void step() {} // empty default step…
Maroš Beťko
  • 2,181
  • 2
  • 16
  • 42
0
votes
1 answer

Why is this MUX with const. inputs not optimised away?

This is a follow-up question to Combinatorial synthesis: Better technology mapping results. I am using Yosys (version 0.5+ (git sha1 f13e387, gcc 5.3.1-8ubuntu2 -O2 -fstack-protector-strong -fPIC -Os)) with the following synthesis…
FriendFX
  • 2,929
  • 1
  • 34
  • 63
0
votes
1 answer

`*' cannot appear in a constant-expression

Could anyone explain me why does those errors exist during my compilation? class ILI9341_due; class LCD_ILI9341 { private: ILI9341_due *tLCD; ILI9341_due &LCD = *tLCD; public: LCD_ILI9341(); ~LCD_ILI9341(); void initDisplay(); …
Klusio
  • 1
  • 1
0
votes
2 answers

In expressions of #if and #elif , what is "defined" prefix for names, and what with identifiers, including C keywords, being replaced by 0?

I tried to wrap my head around it but gave up. Can you please explain the following, taken from the C book by Mike Banahan (Section 7.3.7 Conditional compilation). Despite multiple efforts, I can't grasp the part after "The token sequence that makes…
Thokchom
  • 1,602
  • 3
  • 17
  • 32
0
votes
4 answers

Creating array with constant

I was working on a program in Netbeans on Linux using a gcc compiler when, upon switching to Visual C++ on Windows 7, the code failed to compile as Visual C++ says it expected constant expression on several lines. On Netbeans, I simply did something…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
0
votes
3 answers

Getting the second argument (size) of std::array as a function argument

In the following code, I need to get size of the std::array as a function argument. I preferred std::array to std::vector since the size of the container is not supposed to change. However, the compiler complains as error: ‘n’ is not a constant…
Shibli
  • 5,879
  • 13
  • 62
  • 126
0
votes
0 answers

How to implement a compile time getter, for a more concise call?

I want to implement a compile-time getter in a way to make its call more concise. I have a non-type (unsigned int N) class template foo, which recursively inherits from foo. Every foo has its own enum member called number, which is…
0
votes
1 answer

cannot appear in a constant expression

In the following c++ programm: class matrix { public: int n; double **x; matrix(int n) : n(n) { x=new double[n][n]; for (int i=0;i
0
votes
1 answer

How to avoid getting "constant expression" on if's?

I have an assert macro that resolves to an if, something like this: #define assert(expr) \ if (!(expr)) \ { \ handle_failed_assert(); \ } Ignore how handle_failed_assert() works, and you don't need to cite the do { ... } while(0) trick. Please,…
Spidey
  • 2,508
  • 2
  • 28
  • 38
1 2 3
14
15