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
4
votes
1 answer

Cannot create list literal in F#

I have the following types type StatusCode = | OK = 200 | NoContent = 204 | MovedTemp = 301 | MovedPerm = 302 | SeeOther = 303 | NotModified = 304 | NotFound = 404 | ServerError =…
Overly Excessive
  • 2,095
  • 16
  • 31
4
votes
1 answer

Using a variable in a Java case statment

I am making an expression parser for a calculator. The expressions will contain a variable, for instance, a user could enter "x + 2", or "y^2". I have a switch statement, and one of the cases in switch statement performs a certain action when it…
user1505399
  • 69
  • 1
  • 1
  • 7
4
votes
1 answer

How to get CFBundleShortVersionString as a constant

I append parts to a constant base URL string in my code as such: #define BASE_URL @"https://example.com/developer/" #define PHP_SCRIPT BASE_URL @"index.php" such that the resulting PHP_SCRIPT refers to https://example.com/developer/index.php I am…
Yoga
  • 1,186
  • 1
  • 13
  • 24
3
votes
4 answers

initialize a variable statically (at compile time)

1) I've got many constants in my C algo. 2) my code works both in floating-point and fixed-point. Right now, these constants are initialized by a function, float2fixed, whereby in floating-point it does nothing, while in fixed-point, it finds their…
3
votes
1 answer

Check if a type is defined via static_assert?

I have a situation where I have an enum that defines a list of jobs. enum class job_t { a, b, c, }; Elsewhere I have classes that subclass an interface like class job_interface { public: virtual job_t get_job_type(void) const = 0; …
Tyler
  • 313
  • 3
  • 15
3
votes
2 answers

Constant expression confusion

I am reading an example of cppreference at https://en.cppreference.com/w/cpp/language/constant_expression Specifically, this one: constexpr int incr(int& n) { return ++n; } constexpr int g(int k) { constexpr int x = incr(k); // error:…
3
votes
1 answer

How to iterate over a compile-time seq in a manner that unrolls the loop?

I have a sequence of values that I know at compile-time, for example: const x: seq[string] = @["s1", "s2", "s3"] I want to loop over that seq in a manner that keeps the variable a static string instead of a string as I intend to use these strings…
3
votes
2 answers

Concatenate compile time constant strings with characters whose character code comes from a #define

I want to assemble a char array that contains one byte with a compile time constant value and a string, which is also compile time constant. Solution would be: char packet[] = "\x42" __DATE__; That works but is not very readable and maintainable,…
Stefan Bormann
  • 643
  • 7
  • 25
3
votes
1 answer

Why, when checking compile-time constant, one branch is enough for resolving variable assignation, but is not enough for returning from that branch?

We know, that if if statement's boolean expression/condition contains compile-time constant (or a variable, holding compile-time constant), then compiler can resolve this constant expression and: public void ctc1() { final int x = 1; String…
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
3
votes
3 answers

Initializing very large C++ std::bitset at compile time

I want to store a static constant bitset of 216 bits, with a specific sequence of 1s and 0s that never changes. I thought of using an initializer string as proposed by this post : std::bitset<1<<16> myBitset("101100101000110 ... "); // the ellipsis…
3
votes
1 answer

const fn vs inline attribute

In the following example, I believe that when the function Struct::new is called, its body will be inlined (copied) to the call-site because of the #[inline] attribute. This will result in more code being generated and slower compile times but…
Matej Kormuth
  • 2,139
  • 3
  • 35
  • 52
3
votes
3 answers

"static let" vs "let" for declaring class specific constants

class CurrencyConverter { // 1 private let conversionRate = 1.3 // 2 private static let conversionRate = 1.3 func convertToForeign(fromlocal local: Double) -> Double { return local * CurrencyConverter.conversionRate …
Raunak
  • 3,314
  • 1
  • 22
  • 28
3
votes
1 answer

Use of String.join in Constant Expression

I have a very long String constant which is used for an annotation, and this string is essentially a comma separated list. What I would like to be able to do is the following: String str = String.join(", ", "abc", "def", "ghi",…
Samuel Barr
  • 434
  • 4
  • 12
3
votes
2 answers

Why `switch(null)` is a compile error but `switch(str)` is fine with str being `static final String str = null;`?

While switch(null) is a compile error but switch(str) is fine (str being static final String str = null;). Isn't static final String str = null; a compile-time constant which shall be substituted into switch(str) at compile time and thus be…
Code Complete
  • 3,146
  • 1
  • 15
  • 38
3
votes
2 answers

How is a decompiler able to recognize a compiled constant?

I am using ILSpy to decompile the assemblies of .Net and look into the code. As I was browsing the code of System.Windows.Vector.AngleBetween(Vector, Vector) in WindowsBase.dll, I stumbled across something bizarre. This is the full code of the…
AstroRP
  • 267
  • 6
  • 15