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
8
votes
3 answers

Where to put compile-time-constant arrays?

Say I have an array storing the first 10 primes, like this: const int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; This is all very fine and simple as long as I have 1 .cpp file. However, if I have multiple .cpp files I don't really know where…
Migi
  • 1,112
  • 9
  • 14
8
votes
5 answers

User-defined literal string: compile-time length check

I have a user-defined literal operator that only makes sense for strings of a specific length, like this: constexpr uint16_t operator "" _int(const char* s, std::size_t len) { return len == 2 ? s[0] | (s[1] << 8) : throw; } This works: "AB"_int…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
8
votes
1 answer

How can a variable be both constexpr and not constexpr?

I have made a constexpr string type, which I call StaticString. I got this idea from this website. I am having some weird issues with the compiler treating a variable as a constexpr on one line, and then not a constexpr on the next line. Here is…
DarthRubik
  • 3,927
  • 1
  • 18
  • 54
8
votes
1 answer

Why must default method parameters be compile-time constants in C#

EDIT 1: I know there are alternatives such as telescoping, this was a purely educational question. I know that this is true, but why must it be? It seems like with something like this: public class Foo{ private int bar; public void…
AlphaModder
  • 3,266
  • 2
  • 28
  • 44
8
votes
5 answers

Is there a way to override a Perl "use constant" in your unit testing?

I have a Perl module that I have declared some constants: use constant BASE_PATH => "/data/monitor/"; In live operation the constant will never change but I wish to be able to modify it in my unit tests, e.g. to set it to ~/project/testdata/. Is…
Vagnerr
  • 2,977
  • 3
  • 33
  • 46
7
votes
2 answers

Arithmetic operations between constants

Consider this code; #define A 5 #define B 3 int difference = A - B; does value of "difference" is hardcoded as "2" in compile time, or does it get calculated on runtime?
yasar
  • 13,158
  • 28
  • 95
  • 160
7
votes
3 answers

Clojure compile-time constants

This question comes purely from "mental masterbation" and probably has no practical value. If I define a value in Clojure using def, can the compiler be induced to evaluate it at compile time, and not wait until run-time? (def the-answer 42) (+…
Ralph
  • 31,584
  • 38
  • 145
  • 282
7
votes
2 answers

Why is numeric_limits::max() not equal to -1?

#include #include using namespace std; static_assert(-1 == numeric_limits::max()); // ok static_assert(-1 == numeric_limits::max()); // ok static_assert(-1 == numeric_limits::max()); // error int…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
7
votes
2 answers

final static String defined in an interface not evaluated at compile time - Android

I have two classes and an interface (for example DatabaseModel, LocalStore, and InternalModelInterface). They're defined as follows; public class DatabaseModel { // ... public static final String KEY_PARAM1 = "param1"; } public class LocalStore…
1in9ui5t
  • 126
  • 1
  • 7
7
votes
4 answers

Is it possible to get hash values as compile-time constants?

I thought I'd try selecting different options as strings by hashing them, but this doesn't work: #include #include inline void selectMenuOptionString(const std::string& str) { switch (std::hash()(str)) { …
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
7
votes
3 answers

Is it possible to have a recursive function computed at compile-time in Rust?

I want to compute the factorial of a const: const N: usize = 4; const N_PERMUTATIONS = factorial(N); The solutions I've thought of that don't work in Rust 1.18 are: const fn — conditional statements are not allowed (or at least not implemented) in…
7
votes
3 answers

error: initializer element is not a compile-time constant

I have been looking for answers but could not find anything to make this code run. I get av[1] highlighted by the compiler in the main function when declaring: static char const *str = av[1]; Here is the code I tried to run with gcc: #include…
7
votes
2 answers

Do final vals increase the object size?

class Foo { final val pi = 3 } Does every Foo object have a pi member? Should I therefore put pi in the companion object?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
7
votes
2 answers

Constant expressions from an Enum

Is there any way of converting an enum into a constant expression? I want my switch operator to choose among the values of an enum, but I got a compile error "case expressions must be constant expressions", so I tried to declare it in a…
Luis Sep
  • 2,384
  • 5
  • 27
  • 33
6
votes
1 answer

Contradictory definitions about the Order of Constant Initialization and Zero Initialization in C++

I have been trying to understand how static variables are initialized. And noted a contradiction about the order of constant initialization and zero initialization at cppref and enseignement. At cppref it says: Constant initialization is performed…
user16783784