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

Get type of a module at compile time in F#

We know that in C# we can get type of a type at compile time using typeof(OutType) this lets us pass this to attributes later as it is constant expression. I saw this question, but it doesn't really address the compile time usage. So my question is:…
kuskmen
  • 3,648
  • 4
  • 27
  • 54
2
votes
1 answer

Is there a way to store compile time constant in a class instance?

I was trying to see if I can make a heterogeneous type that can only contain one of several types in its lifetime(an either pattern), and I want to do this: //strong typed (heterogeneous) container template struct STC { …
2
votes
0 answers

Get the id of a declared QMetaType at compile time?

I'm trying to register std::string for use with QVariant, and have it convert to another type which also functions as a variant, but is needed for serialization. I tried to do this with the following code: Q_DECLARE_METATYPE (std::string) const…
Krupip
  • 4,404
  • 2
  • 32
  • 54
2
votes
3 answers

C++ class/structure data member offset as constant expression

Taking offset of a data member is as easy as this: #define MEMBER_OFFSET(Type, Member) \ ((unsigned long)(((char *)&((Type *)0)->Member) - (char *)0)); I want to make this a constant compile-time expression (or use type traits). For example, to…
user405725
2
votes
1 answer

How can I write a C #if directive that branches based on the current date

What I want is something functionally like this: #if TODAY
polytopia
  • 419
  • 4
  • 11
2
votes
2 answers

Externally defined constant in C header file

I have a struct that is defined within a header file that contains a 2D array (lanes). I would like to define the size of the array at compile time, for example by setting an environment variable. #ifndef GAMEBOARD_H #define GAMEBOARD_H struct…
N. Elich
  • 23
  • 3
2
votes
0 answers

Building OpenCV 3.2.0 with MinGW-w64 6.1.0: compile-time argument evaluation faiIure

The compiler fails with this output: In file included from C:/mingw-w64/x86_64-6.1.0-win32-seh-rt_v5-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/6.1.0/include/emmintrin.h:31:0, from…
Alex
  • 1,165
  • 2
  • 9
  • 27
2
votes
2 answers

Compile-time const and formal parameters

In the following example: static inline void foo(const int varA) { ... __some_builtin_function(varA); ... } int main() { foo(10); return 0; } Is varA here considers as a compile-time constant? Please note that I am working with C, not…
Arsen
  • 654
  • 8
  • 20
2
votes
2 answers

C++ compile-time bitmask addition

I have a number of bitmasks to add (layer, logical OR |), but as they are constants I would like to do so at compile time. Entering advanced template territory... I tried recursion: template struct MaskAdd { …
Kisss256
  • 25
  • 7
2
votes
1 answer

how to initialize a large array in Fortran?

I have a Fortran function in which I would like to initialize a large array at compile time. A simplified working example is below, where the parameter coeff in fill_coefficients has been reduced in size greatly. How do I write similar code when…
knia
  • 463
  • 6
  • 16
2
votes
3 answers

How to force GCC compiler to calculate constants at compile-time with -Os

I tried to calculate hashes for constant C-strings in compile-time using macros. That is my example code: #include #include typedef uint32_t hash_t; #define hash_cstr(s) ({ \ typeof(sizeof(s)) i = 0; \ …
2
votes
1 answer

Array Constants (Fields) as Annotation Values

While this code happily compiles (with Java 8 / Eclipse Compiler) public @interface specialized { public Class[] value() default { int.class, long.class, float.class, double.class }; } Refactoring it to use a constant instead of an array causes…
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
2
votes
2 answers

Check some compile-time definitions at compile time with older C++ implementations

When working on a large legacy code base, I today suspected a duplicate definition, but the dependency was not obvious to me human since it depended on a lots of compile-time calculations. enum { MAX_ITEMS = 4 }; // defined somewhere in my code…
Wolf
  • 9,679
  • 7
  • 62
  • 108
2
votes
2 answers

Static Final Ints in Switch: Why can't this be done?

I had a Switch referencing Resource Ids from R.java in a Library Project: switch (code) { case R.id.code_one: blah(); break; case R.id.code_two: bleh(); break; } From ADT 14, R fields are no longer final, so…
2
votes
1 answer

C++ compile time unique ID generator always returns same value

I was trying to create a compile time hasher that takes a string literal and creates an integer. I'm using Code::Blocks's GCC 4.7.1 compiler (which broke on me earlier). This is how it works: typedef unsigned long long ull; //less typing constexpr…
Bob
  • 53
  • 2