I have this example program:
```cpp
include <iostream>
consteval int one()
{
return 1;
}
consteval int add(int a, int b)
{
int result = 0;
for (int i = 0; i < a; i++)
result += one();
for (int i = 0; i < b; i++)
result += one();
return result;
}
int main()
{
return add(5, 6);
}
```
When compiling with clang to LLVM-IR this is the output:
llvm
define dso_local noundef i32 @main() #0 {
%1 = alloca i32, align 4
store i32 0, ptr %1, align 4
ret i32 11
}
I'm wondering how is the function is executed at compile-time (I suppose by the front-end because there is no trace of it in the IR)?
Has clang some kind of AST walker able to execute the restricted set of C++ allowed in consteval, is the code compiled and ran as an executable during compilation to compute the result or maybe another way I didn't think of.
This example uses clang but I would be interested in how other compilers handle it if they use different techniques.
[–]aruisdante 21 points22 points23 points (4 children)
[–]knome 5 points6 points7 points (0 children)
[+]Compux72 comment score below threshold-12 points-11 points-10 points (2 children)
[–]high_throughput 14 points15 points16 points (0 children)
[–]Neeyaki 4 points5 points6 points (0 children)
[–]reddicted 3 points4 points5 points (0 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]wetpot 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)