aboutsummaryrefslogtreecommitdiff
path: root/articles/2015-01-14_a_look_at_compile_time_computation_in_cpp.md
diff options
context:
space:
mode:
Diffstat (limited to 'articles/2015-01-14_a_look_at_compile_time_computation_in_cpp.md')
-rw-r--r--articles/2015-01-14_a_look_at_compile_time_computation_in_cpp.md20
1 files changed, 8 insertions, 12 deletions
diff --git a/articles/2015-01-14_a_look_at_compile_time_computation_in_cpp.md b/articles/2015-01-14_a_look_at_compile_time_computation_in_cpp.md
index 18de33f..7a5ce3a 100644
--- a/articles/2015-01-14_a_look_at_compile_time_computation_in_cpp.md
+++ b/articles/2015-01-14_a_look_at_compile_time_computation_in_cpp.md
@@ -8,7 +8,7 @@ Besides C-style macros C++ templates present one additional language element tha
My first attempt at facilitating compile time computation is a _functional-style_ list library based on template metaprogramming: [ConstList]. This library handles lists in a fashion simmilar to how it is done in languages such as Scheme or Haskell, i.e. by providing functions such as `fold` and `map` which manipulate a basic list type based on _Cons_ expressions. As an example one may consider how [ConstList]'s [`map`] function is expressed in terms of [`foldr`]:
-~~~
+```cpp
template <
typename Cons,
typename Function
@@ -22,14 +22,13 @@ constexpr auto map(const Cons& cons, const Function& function) {
make()
);
}
-~~~
-{: .language-cpp}
+```
The [`foldr`] implementation is also quite straightforward and simply applies a given function to each pair of the _Cons_ structure using static recursion. Note that this approach of _lambda expression based_ template metaprogramming would have been much more verbose in C++11 as many list manipulators such as [`map`] and [`foldr`] make use of C++14's _generic lambda expressions_. While the [test cases] provide a set of - in my opinion - reasonably nice list transformations and queries they also present the core problem of the particular approach taken in [ConstList], as it is impossible to return lists of varying lengths depending on their contents. This pervasive limitation exists because the only way to vary types at compile time depending on values is to use these values as template parameters. That is the _Cons_ list type tree would have to be both list declaration and definition, analogously to e.g. [`std::integral_constant`]. Obviously this is quite different from how types and values were separated into templates and member constants in [ConstList]. One would have to think of types as values and templates as functions that modify those values instead.
Furthermore the compilation performance degrades noticeably when manipulating lists with more than a couple of dozen items or plainly fails to execute at compile time at all. What works in a reasonably consistent fashion are list manipulations such as this one, which evaluates down to a hard coded `1056` in GCC's Assembler output:
-~~~
+```cpp
#include "list.h"
int main(int, char**) {
@@ -48,8 +47,7 @@ int main(int, char**) {
0
);
}
-~~~
-{: .language-cpp}
+```
To summarize: The approach taken in my implementation of [ConstList] may be a nice exercise in template metaprogramming and writing _functional-style_ C++ code but its practical applications in compile time computation are unreasonably narrow.
@@ -57,7 +55,7 @@ To summarize: The approach taken in my implementation of [ConstList] may be a ni
As was already mentioned, prior to the C++11 standard the only way to perform compile time computations was to rely on macros and template metaprogramming. While both of those can be thought of as separate _functional-style_ languages inside C++, the `constexpr` keyword allows one to declare a normal function as _potentially executable_ at compile time. So contrary to template metaprogramming based solutions we don't have a strong guarantee that our _compile time program_ is actually evaluated at compile time and would have to look at the generated Assembler output when in doubt. Sadly this is actually not much more than what is possible in _normal_ C++ compiled by a _sufficiently smart compiler_, e.g. the listing below is evaluated at compile time by GCC without any usage of `constexpr` or template metaprogramming:
-~~~
+```cpp
int example(int x) {
return 2 * x;
}
@@ -75,17 +73,15 @@ int main(int, char**) {
recursive_example(21)
);
}
-~~~
-{: .language-cpp}
+```
Where the verbose Assembler output is acquired as follows (note that the same command also works for Clang):
-~~~
+```sh
> g++ -S -fverbose-asm -O2 example.cc -o example.asm
> grep example.asm -n -e 42
95: movl $42, %eax #,
-~~~
-{: .language-sh}
+```
One area where the example above would not work and one would thus require the `constexpr` keyword, is when one wants to use the result of a function as e.g. a template parameter or any other value that is required by the standard to be defined at compile time. While this is certainly useful it - contrary to what one could think after first hearing about `constexpr` - doesn't quite enable one to explicitly write _compile time programs_ in the same way as _normal_ programs.