Questions tagged [compile-time-constant]

Use this tag for questions related to the compile time constant, a constant value that is known at compile time.

A expression is an expression denoting a value of primitive type or a String that is composed using only the following:

is uses in its general meaning, but you should provide the relevant tag of your programming environment, if any.

300 questions
221
votes
14 answers

Java switch statement: Constant expression required, but it IS constant

So, I am working on this class that has a few static constants: public abstract class Foo { ... public static final int BAR; public static final int BAZ; public static final int BAM; ... } Then, I would like a way to get a…
Austin Hyde
  • 26,347
  • 28
  • 96
  • 129
80
votes
6 answers

How to declare a constant Guid in C#?

Is it possible to declare a constant Guid in C#? I understand that I can declare a static readonly Guid, but is there a syntax that allows me to write const Guid?
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
57
votes
9 answers

constexpr overloading

Related: Function returning constexpr does not compile I feel like constexpr is limited in usefulness in C++11 because of the inability to define two functions that would otherwise have the same signature, but have one be constexpr and the other not…
David Stone
  • 26,872
  • 14
  • 68
  • 84
53
votes
7 answers

M_PI flagged as undeclared identifier

When I compile the code below, I got these error messages: (Error 1 error C2065: 'M_PI' : undeclared identifier 2 IntelliSense: identifier "M_PI" is undefined) What is this? #include #include using namespace std; double…
Eunsu Kim
  • 731
  • 1
  • 5
  • 9
52
votes
8 answers

Compile-time constants and variables

The Java language documentation says: If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value. This is called a compile-time …
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
45
votes
3 answers

Why isn't a final variable always a constant expression?

In the below code: final int a; a=2; byte b=a; // error: possible loss of precision Why do I get this error? Isn't a final variable compile time constant expression and hence implicitly narrowed to byte during the assignment? In other words isn't…
paidedly
  • 1,413
  • 1
  • 13
  • 22
43
votes
5 answers

Dividing by zero in a constant expression

My toy compiler crashes if I divide by zero in a constant expression: int x = 1 / 0; Is this behaviour allowed by the C and/or C++ standards?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
41
votes
3 answers

What's the difference between equ and db in NASM?

len: equ 2 len: db 2 Are they the same, producing a label that can be used instead of 2? If not, then what is the advantage or disadvantage of each declaration form? Can they be used interchangeably?
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
34
votes
4 answers

Why isn't std::string::max_size a compile-time constant?

std::string provides a max_size() method to determine the maximum number of elements it can contain. However, to work out the maximum length of a string in general, the programmer has to create a (possibly empty) string object. If this class doesn't…
slaphappy
  • 6,894
  • 3
  • 34
  • 59
30
votes
3 answers

Constant expression contains invalid operations

I have the following code, where I get the error "PHP Fatal Error: Constant expression contains invalid operations". It works fine when I define the variable in the constructor. I am using Laravel framework.
Aaron
  • 759
  • 1
  • 7
  • 15
29
votes
2 answers

C# - Why are DateTime.MinValue and MaxValue not compile-time constants?

I wanted to have an optional date parameter for a method (defaulted to MinValue), in order to check if the user had actually supplied a value or not (supplying MinValue was invalid), but I'm not allowed as apparently it's not a compile-time…
Alex
  • 2,681
  • 3
  • 28
  • 43
28
votes
2 answers

Compiletime build up of std::regex

Since I know the regexes at compiletime, and building up a regex is in O(2^m) where m is the length of the regex, I would love to build up the regex at compiletime. Is this possible with std::regex? (I don't think so, because I don't see any…
Exagon
  • 4,798
  • 6
  • 25
  • 53
28
votes
5 answers

Difference between final variables and compile time constant

What is the difference between final variables and compile time constants? Consider the following code final int a = 5; final int b; b=6; int x=0; switch(x) { case a: //no error case b: //compiler error } What does this mean? When and how…
Tarun Mohandas
  • 765
  • 1
  • 8
  • 15
25
votes
6 answers

Are all compile-time constants inlined?

Let's say I have a class like this: class ApplicationDefs{ public static final String configOption1 = "some option"; public static final String configOption2 = "some other option"; public static final String configOption3 = "yet another…
Saravanan M
  • 4,697
  • 5
  • 35
  • 37
22
votes
7 answers

Delphi: All constants are constant, but some are more constant than others?

Consider: const clHotlight: TColor = $00FF9933; clLink = clHotLight; //alias of clHotlight [Error] file.pas: Constant expression expected and the alternate wording that works: const clHotlight = TColor($00FF9933); clLink =…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
1
2 3
19 20