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

Why is it common to use dynamic errors in rust, and not enums? Is it bad/not possible to use compile-time variants?

I'm seeing this trend in Rust that errors in Result are returned like this: fn do_something() -> Result> { // ... } Why are errors dynamic? Having come from a C++ background and prefering std::variant over…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
14
votes
2 answers

Finding endian-ness programmatically at compile-time using C++11

I have referred many questions in SO on this topic, but couldn't find any solution so far. One natural solution was mentioned here: Determining endianness at compile time. However, the related problems mentioned in the comments & the same…
iammilind
  • 68,093
  • 33
  • 169
  • 336
14
votes
5 answers

Implementing a compile-time "static-if" logic for different string types in a container

I'd like to write a function template that operates on a container of strings, for example a std::vector. I'd like to support both CString and std::wstring with the same template function. The problem is that CString and wstring have different…
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
14
votes
3 answers

Using CRC32 algorithm to hash string at compile-time

Basically I want in my code to be able to do this: Engine.getById(WSID('some-id')); Which should get transformed by Engine.getById('1a61bc96'); just before being compiled into asm. So at compile-time. This is my try constexpr int WSID(const…
Vinz243
  • 9,654
  • 10
  • 42
  • 86
14
votes
3 answers

Determining struct member byte-offsets at compile-time?

I want to find the byte offset of a struct member at compile-time. For example: struct vertex_t { vec3_t position; vec3_t normal; vec2_t texcoord; } I would want to know that the byte offset to normal is (in this case it should be…
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
14
votes
3 answers

What is the difference between runtime and compile-time?

So what is a runtime? Is it a virtual machine that executes half-compiled code that cannot run on a specific processor. If so, then what's a virtual machine? Is it another software that further translates the half-compiled code to machine specific…
Garrett Biermann
  • 537
  • 1
  • 4
  • 12
14
votes
2 answers

Scala slow builds: development approaches to avoid

First of all, incremental builds via SBT are pretty awesome, generally in the < 1sec range. However, sometimes you have to do a full clean/compile, or, in the case of incremental builds, you make a change to one file which then triggers the…
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
13
votes
2 answers

Compile-time assertions with GHC Haskell?

Coming from C++, I'm used to be able to build simple forms of compile-time assertions, where I could emit warnings or errors during compilation if some simple conditions (e.g. over simple algebraic expressions) weren't met via use of template…
hvr
  • 7,775
  • 3
  • 33
  • 47
13
votes
1 answer

Force pre-computation of a constant

I have a constant declaration in Haskell -- can I force this to be evaluated ahead of time? I'm seeing some code that looks roughly like, myList = [(a, b), (c, d)] ... map (f . fst) myList take time in the fst call when I profile it (it does have…
13
votes
1 answer

How to get compile time type of a variable?

I'm looking for how to get compile time type of a variable for debugging purposes. The testing environment can be reproduced as simply as: object x = "this is actually a string"; Console.WriteLine(x.GetType()); Which will output System.String. How…
tomsseisums
  • 13,168
  • 19
  • 83
  • 145
12
votes
3 answers

Check at Compile-Time if Template Argument is void

I'm trying to wrap the Windows API functions to check errors when I so choose. As I found out in a previous SO question, I could use a template function to call the API function, and then call GetLastError() to retrieve any error it might have set.…
chris
  • 60,560
  • 13
  • 143
  • 205
12
votes
2 answers

Why are function addresses not constant expressions

Is there a way to use function addresses in constant expressions? void foo() {} int main() { static_assert(&foo, "test error"); } This won't compile. error C2057: expected constant expression The intention behind this is that I want to…
cooky451
  • 3,460
  • 1
  • 21
  • 39
12
votes
2 answers

Compile time evaluation

If I write enum chars = digits ~ uppercase; will the string be concatenated at compile time? I'm assuming it will. If I replace it with a string literal or a CTFE function I can't measure any significant performance differences (even calling it a…
fwend
  • 1,813
  • 2
  • 15
  • 17
12
votes
2 answers

Understanding Raku's `&?BLOCK` compile-time variable

I really appreciate the Raku's &?BLOCK variable – it lets you recurse within an unnamed block, which can be extremely powerful. For example, here's a simple, inline, and anonymous factorial function: { when $_ ≤ 1 { 1 }; $_ × &?BLOCK($_ - 1)…
codesections
  • 8,900
  • 16
  • 50
12
votes
3 answers

How to implement a compile-time [dispatch] table for AVR?

I have the same prerequisites as Dave Durbin in How can I implement a dynamic dispatch table in C... except my target is AVR. Here are my constraints: modules are to be picked in a list, much like Linux compiled-in kernel modules the number of C…
user4113344