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
1
vote
1 answer

Initializing correct union member based on compile time evaluation

I want to initialize different union member depending if a macro expression is a constant or a variable. So far I've found GCC extension __builtin_constant_p() which evaluates an expression and returns 1 if it is a constant and 0 otherwise. Which is…
user1806687
  • 934
  • 1
  • 10
  • 27
1
vote
0 answers

Capture argument to addition operator as a template parameter

In the following example, I define a basic statically-sized container class. I want to specifically use integer arithmetic to expand the size of the container. Consider the following example (that does not compile when the line auto arr2 = arr+3; is…
wvn
  • 624
  • 5
  • 12
1
vote
1 answer

Rust: cannot call non-const fn `::default` in constants

I'm trying to create a global instance that I can borrow a reference too, const GLOBAL_FOO: &Foo = &Foo::default(); impl<'a> Default for Bar<'a> { fn default() -> Self { Self { foo: GLOBAL_FOO } } } But when I do this, I get an…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
1
vote
0 answers

Referencing Associated Constants with generic constants

I have the following code so far: const WORD_SIZE: usize = std::mem::size_of::() * 8; pub struct Matrix { pub rows: [[usize; N]; M] } impl Matrix { const N_words:…
user2154420
  • 442
  • 4
  • 17
1
vote
0 answers

String concatenation of compile time constants

I'm trying to understand the String concatenation process. The below initialized variables (a, b, str) are all compile time constants. When concatenating the compile time constants and comparing it with the equivalent string literals, it should…
Siva
  • 55
  • 1
  • 6
1
vote
1 answer

Is a string literal converted to bool valid as part of a constant initializer expression?

enum { compile_time_assertion = 1 / ("description" && 1); }; Does the above snippet conform to C11? Having a string literal as part of the expression may violate one of the many constraints for an initializer expression. 6.6p7 lists an address…
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
1
vote
1 answer

Objective-c constant static NSArray

I am a Java programmer, learning Objective-C and I have a problem with implementation of variables, similar to static final class variables in Java. In class PolygonShape, I would like to have NSDictionary with polygon types, which can be accessed…
Pavle Gartner
  • 659
  • 1
  • 7
  • 21
1
vote
3 answers

accessing std::tuple element by constexpr in type

I would like to access to a tuple element at compile time by a value constexpr in the type #include #include #include struct A { static constexpr int id = 1; void work() { std::cout << "A" <<…
Deub
  • 23
  • 4
1
vote
2 answers

Compile-time ceiling function, for literals, in C?

I have a C program which involves some floating-point literal that's defined by a macro. And - it just so happens that in a performance-critical part of the code, I need the ceiling of that floating-point number, as an integer (say, an int). Now, if…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1
vote
1 answer

How to create a compile time const string "n" characters long for NUnit Testcase

Using NUnit I want to test whether given string is shorter than 200 characters. My goal is not to hard-code this string, because I will have a string with 201 characters as counter-part in another test. Lets look at the test…
Korfu
  • 673
  • 5
  • 20
1
vote
2 answers

How to set the size validator of a class property to a known constant

I would like to do something like the following code where all 3 properties are statically known to be size (1,10) without having to explicitly re-write the 10 at the property declaration. classdef Example properties(Constant) …
1
vote
0 answers

In which order are constant functions evaluated when assigned to constant variables?

When there are multiple constant variables in a rust program, and they get initialized (on compile time) by a constant function, in which order are they initialized? How can I assure safety if the initialization of one depends on the other? For…
Fowlron
  • 99
  • 1
  • 11
1
vote
1 answer

Static constexpr data member initialized off-class, and a possible workaround?

From elsewhere I know that it's not possible in C++17 to declare a static constexpr data member without its immediate initialization (although yet elsewhere they use such example). // --- in header --- struct Data{ LPCTSTR str; int…
tomascz
  • 235
  • 1
  • 13
1
vote
2 answers

Sparse matrix-dense vector multiplication with matrix known at compile time

I have a sparse matrix with only zeros and ones as entries (and, for example, with shape 32k x 64k and 0.01% non-zero entries and no patterns to exploit in terms of where the non-zero entries are). The matrix is known at compile time. I want to…
Adomas Baliuka
  • 1,384
  • 2
  • 14
  • 29
1
vote
1 answer

alignas with pack expansion not working in Visual Studio 2019?

I'm writing a simple implementation for a Variant class, but Visual Studio rejects this code when I try to instantiate a variable of type AlignedStorage: using std::size_t; template struct AlignedStorage { …
Luca
  • 1,658
  • 4
  • 20
  • 41