Questions tagged [compile-time]

Refers to the information that can be inferred or known at the time source code is compiled, as opposed to information that can only be inferred when source code is run. Do not use this tag for questions about the time it takes for source code to be compiled.

In , compile time refers to either the operations performed by a (the "compile-time operations"), requirements that must be met by source code for it to be successfully compiled (the "compile-time requirements"), or properties of the program that can be reasoned about at compile time.

The operations performed at compile time usually include , various kinds of (e.g., type checks and instantiation of template) and .

Programming language definitions usually specify compile time requirements that must meet to be successfully compiled. For example, that the amount of storage required by types and variable can be deduced.

Properties of a program that can be reasoned about at compile time include range-checks (e.g., proving that an array index will not exceed the array bound), deadlock freedom in concurrent languages, or timings (e.g., proving that a sequence of code takes no more than an allocated amount of time).

Compile time occurs before (when the output of one or more compiled files are joined together) and runtime (when a program is executed).

In some programming languages it may be necessary for some compilation and linking to occur at runtime.

There is a trade-off between compile-time and link-time in that many compile time operations can be deferred to link-time without incurring extra run-time.

"Compile time" can also refer to the amount of time required for compilation.

Source: Wikipedia

925 questions
11
votes
2 answers

Scala - Enforcing size of Vector at compile time

Is it possible to enforce the size of a Vector passed in to a method at compile time? I want to model an n-dimensional Euclidean space using a collection of points in the space that looks something like this (this is what I have now): case class…
adelbertc
  • 7,270
  • 11
  • 47
  • 70
11
votes
1 answer

Forcing a constant expression to be evaluated during compile-time?

A few days ago I asked by which criteria the compiler decides whether or not, to compute a constexpr function during compile time. When does a constexpr function get evaluated at compile time? As it turns out, a constexpr is only evaluated during…
Byzantian
  • 3,208
  • 4
  • 27
  • 31
11
votes
5 answers

Compiletime validation of enum parameters

There is a constructor with three parameters of type enum: public SomeClass(EnumType1 enum1,EnumType2 enum2, EnumType3 enum3) {...} The three parameters of type enum are not allowd to be combined with all possible…
11
votes
4 answers

How to check `typeof` for void value at compile time?

let's say that I want to have C macro that works on any type. I'm using GCC compiler (>= 4.6) and can use GNU99 macros. //code... any_type_t *retVal = function_that_runs_very_long_time(a, b, &&c, **d, &e, *f); //other code... usage of macro for…
DinGODzilla
  • 1,611
  • 4
  • 21
  • 34
10
votes
1 answer

C++ type id at compile time

I want to generate a hash for a class based on its derived type at compile time. Today I generate it like: template class TypeBase { public: static const unsigned s_kID; }; template const unsigned TypeBase::s_kID…
Robert
  • 2,330
  • 29
  • 47
10
votes
1 answer

What are "extern char condition tricks"?

I was reading the GCC documentation on C and C++ function attributes. In the description of the error and warning attributes, the documentation casually mentions the following "trick": error ("message") warning ("message") If the error or warning…
Boann
  • 48,794
  • 16
  • 117
  • 146
10
votes
2 answers

What is the _intended_ value of &?ROUTINE?

The Rakudo implementation of Raku tracks multiple issues about the (very useful!) &?ROUTINE variable not providing the correct value (e.g., #1768 and 2362), so I realize that it's not behaving quite correctly. But I'm trying to understand what it's…
codesections
  • 8,900
  • 16
  • 50
10
votes
1 answer

CONTROL phasers from a trait

Is it possible to add a CONTROL phaser from a trait? Following the example from the docs, it's simple to add a custom control exception in runtime code: class CX::Oops does X::Control {}; sub f { CONTROL { when CX::Oops { note 'Oops!'; .resume}} …
codesections
  • 8,900
  • 16
  • 50
10
votes
4 answers

Ensure mutually exclusive interfaces at compile-time?

I'd like to ensure that two interfaces are never found on the same class at compile-time, similar to how AttributeUsage checks custom Attributes at compile-time. e.g.: [InterfaceUsage(MutuallyExclusive = typeof(B))] interface A { …
Iain Sproat
  • 5,210
  • 11
  • 48
  • 68
10
votes
2 answers

In Rust, can I instantiate my const array without hard-coding in the values? Compile-time evaluation?

I'm trying to instantiate an array in Rust. Here's one way I could do it at runtime: let mut t = [0_u32; 65]; for i in 0..t.len() { t[i] = ((i as f64).sin().abs() * 2.0_f64.powf(32.0)).floor() as u32; } However, since I'm never going to change…
user457586
10
votes
5 answers

The mechanics of extension via free functions or member functions

Loads of C++ libraries, the standard included, allow you to adapt your objects for use in the libraries. The choice is often between a member function or a free function in the same namespace. I'd like to know mechanics and constructs the library…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
10
votes
2 answers

How do I get the method name at compile time?

How do I get the enclosing method name at compile time? And why has it been difficult for me to figure this out? Why wouldn't Java want me to do this? I don't see any inherent problems with a feature like this, and it's useful for logging method…
Mike
  • 201
  • 2
  • 4
10
votes
1 answer

How can I cast a back to a type a value was before?

Very often when writing generic code in F# I come by a situation similar to this (I know this is quite inefficient, just for demonstration purposes): let isPrime n = let sq = n |> float |> sqrt |> int {2..sq} |> Seq.forall (fun d -> n % d <>…
primfaktor
  • 2,831
  • 25
  • 34
10
votes
4 answers

[C++ compile time assertions]: Can we throw a compilation error if some condition is not met?

I wrote a function: template void tryHarder() { for(int i = 0; i < N; i++) { tryOnce(); } } but I only want it to compile if N is in between 0 and 10. Can I do it? How?
MciprianM
  • 513
  • 1
  • 7
  • 18
9
votes
8 answers

Compile time recursion and conditionals

I was reading the responses to "Printing 1 to 1000 without loop or conditionals" and I am wondering why it is necessary to have the special case for NumberGeneration<1> in the top answer. If I remove that and add a check for N == 1 in the template…
baris.m
  • 93
  • 4