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
23
votes
3 answers

Some const char * are unavailable at compile time?

Let's suppose we have a template function with non-type parameter of const char * like this: template void print() { std::cout << MESSAGE << '\n'; } Using this template wouldn't be a problem as log as the MESSAGE can be…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
22
votes
4 answers

Compile Time Reflection in C#

I frequently write C# code that has to use magic strings to express property names. Everyone knows the problems with magic strings. They are very difficult to refactor, they have no compile time checking, and often they lead to hard-to-diagnose…
MgSam
  • 12,139
  • 19
  • 64
  • 95
22
votes
5 answers

How compile time recursion works?

I found a code here Printing 1 to 1000 without loop or conditionals Can someone please explain how compile time recursion works, couldn't find it in google // compile time recursion template void f1() { f1(); cout << N << '\n';…
VextoR
  • 5,087
  • 22
  • 74
  • 109
21
votes
2 answers

Compile-time or runtime detection within a constexpr function

I was excited when constexpr was introduced in C++11, but I unfortunately made optimistic assumptions about its usefulness. I assumed that we could use constexpr anywhere to catch literal compile-time constants or any constexpr result of a literal…
Kumputer
  • 588
  • 1
  • 6
  • 22
21
votes
1 answer

In PHP, what is meant by compile-time and run-time?

PHP is an interpreted language, not compiled. Yet I have come across a book that mentions stuff happening in PHP at compile time, and the PHP manual states that declaring a const happens at compile-time. How is the term compile-time used in relation…
TigerBear
  • 2,479
  • 1
  • 21
  • 24
21
votes
2 answers

Check for framework's existence at compile time?

I'm working on an open-source project that can optionally use a closed-source framework. If the closed-source framework is included in the project, there will be additional functionality. But if the framework isn't included in the project, the…
johngraham
  • 6,461
  • 2
  • 16
  • 22
20
votes
1 answer

Is there a way to build C++ custom qualifiers?

Is there any way to implement a custom type qualifier (similar to const)? I would like to only allow function calls to functions that are of the right qualification, within functions with the same qualification. Let's say I would have: void…
Andreas Loanjoe
  • 2,205
  • 10
  • 26
20
votes
9 answers

Compile-time sizeof conditional

I want to define a macro if a condition involving sizeof is true and do nothing (but still compile) if it is false. If the preprocessor supported sizeof, it would look like this: #if (sizeof(void*) <= sizeof(unsigned int)) // what goes here? # …
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
20
votes
6 answers

Output compile time stamp in Visual C++ executable?

How can I insert compilation timestamp information into an executable I build with Visual C++ 2005? I want to be able to output something like this when I execute the program: This build XXXX was compiled at dd-mm-yy, hh:mm. where date and time…
Michael
20
votes
5 answers

Does "undefined behaviour" extend to compile-time?

We've all heard the warnings that if you invoke undefined behaviour in C or C++, anything at all can happen. Is this limited to any runtime behaviour at all, or does this also include any compile-time behaviour? In particular, is a compiler, upon…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
19
votes
2 answers

Why are 'constexpr' parameters not allowed?

It would be useful to have 'constexpr' parameters in order to distinguish compiler-known values and so be able to detect errors at compile time. Examples: int do_something(constexpr int x) { static_assert(x > 0, "x must be > 0"); return x +…
AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66
19
votes
7 answers

Compile time computing of number of bits needed to encode n different states

Edit: In the initial question had a wrong formula and the algorithm tried was doing something completely different than what was intended. I apologise and I decided to rewrite the question to eliminate all the confusion. I need to compute at compile…
bolov
  • 72,283
  • 15
  • 145
  • 224
18
votes
3 answers

Is `auto` specifier slower in compilation time?

Since C++11 we can use auto a = 1+2 instead of int a = 1+2 and the compiler deduces the type of a by itself. How does it work? Is it slower during compile time (more operations) than declaring the type myself?
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
18
votes
2 answers

Is static_cast(...) compile-time or run-time?

Is static_cast(...) something that gets done at compile-time or run-time? I've googled around but I got different answers. Also, dynamic_cast(...) is obviously runtime - but what about reinterpret_cast(...)?
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
17
votes
1 answer

Is it possible to validate the input to a user-defined literal at compile time

In the following example I would like to be told at compile time that the conversion from long to int changes the value just like I do if I don't use the user defined literal. #include constexpr int operator "" _asInt(unsigned long long…
sji
  • 1,877
  • 1
  • 13
  • 28
1 2
3
61 62