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
41
votes
6 answers

How to reduce compile time with C++ templates

I'm in the process of changing part of my C++ app from using an older C type array to a templated C++ container class. See this question for details. While the solution is working very well, each minor change I make to the templated code causes a…
SmacL
  • 22,555
  • 12
  • 95
  • 149
40
votes
3 answers

Is std::less supposed to allow comparison of unrelated pointers at compile-time?

Consider this code: #include #include template inline constexpr const void *foo = &typeid(T); int main() { constexpr bool a = std::less{}(foo, foo); } Run on gcc.gotbolt.org If I…
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
40
votes
3 answers

Initialize an std::array algorithmically at compile time

Consider: static constexpr unsigned num_points{ 7810 }; std::array< double, num_points > axis; for (int i = 0; i < num_points; ++i) { axis[i] = 180 + 0.1 * i; } axis is a class-wide constant. I want to avoid initializing it like any other…
Vorac
  • 8,726
  • 11
  • 58
  • 101
39
votes
4 answers

How do I switch/select types during compile-time?

Is there a standard way for me to select a type at compile-time on an unsigned index in c++11? For example, something like: using type_0 = static_switch<0, T, U>; // yields type T using type_1 = static_switch<1, T, U>; // yields type U If there…
kfmfe04
  • 14,936
  • 14
  • 74
  • 140
36
votes
5 answers

Is is_constexpr possible in C++11?

Is it possible to produce a compile-time boolean value based on whether or not a C++11 expression is a constant expression (i.e. constexpr) in C++11? A few questions on SO relate to this, but I don't see a straight answer anywhere.
user2023370
  • 10,488
  • 6
  • 50
  • 83
35
votes
6 answers

General rules of passing/returning reference of array (not pointer) to/from a function?

We can pass reference of an array to a function like: void f(int (&a)[5]); int x[5]; f(x); //okay int y[6]; f(y); //error - type of y is not `int (&)[5]`. Or even better, we can write a function template: template void f(int…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
35
votes
4 answers

Is there any advantage in using static_cast rather than C-style casting for non-pointer types?

I am well aware of the advantage in using static_cast rather than C-style casting for pointer types. If the pointer types are incompatible, then: static_cast will yield a compile-time error at a specific line within the source code C-style casting…
barak manos
  • 29,648
  • 10
  • 62
  • 114
34
votes
7 answers

Is it possible to test if a constexpr function is evaluated at compile time?

Since the extended versions of constexpr (I think from C++14) you can declare constexpr functions that could be used as "real" constexpr. That is, the code is executed at compile time or can behave as inline functions. So when can have this…
LeDYoM
  • 949
  • 1
  • 6
  • 21
29
votes
17 answers

Detecting Endianness

I'm currently trying to create a C source code which properly handles I/O whatever the endianness of the target system. I've selected "little endian" as my I/O convention, which means that, for big endian CPU, I need to convert data while writing or…
Cyan
  • 13,248
  • 8
  • 43
  • 78
29
votes
6 answers

How To Get the Name of the Current Procedure/Function in Delphi (As a String)

Is it possible to obtain the name of the current procedure/function as a string, within a procedure/function? I suppose there would be some "macro" that is expanded at compile-time. My scenario is this: I have a lot of procedures that are given a…
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
26
votes
5 answers

Conditional compile-time inclusion/exclusion of code based on template argument(s)?

Consider the following class, with the inner struct Y being used as a type, eg. in templates, later on: template class X{ template struct Y{}; template struct Y{}; }; Now, this example will obviously…
Xeo
  • 129,499
  • 52
  • 291
  • 397
26
votes
4 answers

Calculating and printing factorial at compile time in C++

template struct Factorial { enum { value = n * Factorial::value}; }; template<> struct Factorial<0> { enum {value = 1}; }; int main() { std::cout << Factorial<5>::value; std::cout <<…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
26
votes
5 answers

Why is char[][] = {{...}, {...}} not possible if explicitly given a multidimensional array?

I went through this article. I understand the rules explained but I am wondering what exactly blocks the compiler from accepting the following syntax when defining a constant multi-dimensional array and directly initializing it with known values of…
esgaldir
  • 823
  • 9
  • 11
25
votes
5 answers

Can I make a constant from a compile-time env variable in C#?

We use Hudson to build our projects, and Hudson conveniently defines environment variables like %BUILD_NUMBER% at compile time. I'd like to use that variable in code, so we can do things like log what build this is at run time. However I cannot do…
Eggplant Jeff
  • 1,749
  • 2
  • 15
  • 20
25
votes
5 answers

What is compile-time polymorphism and why does it only apply to functions?

What is compile-time polymorphism and why does it only apply to functions?
ihtkwot
  • 1,252
  • 4
  • 16
  • 36
1
2
3
61 62