C++ templates — compilation time vs execution time
Date: 2020-12-16Last modified: 2024-11-02
Table of contents
Will be evaluated in execution time:
template <typename T>
T cube(const T &value) {
return value * value * value;
}
Will be evaluated in compilation time:
template <int toCube>
struct Cube {
enum { value = toCube * toCube * toCube };
};
fmt::print("8 cubed = {} (execution time)\n", int(cube(8)));
fmt::print("5 cubed = {} (compilation time)\n", int(Cube<5>::value));
Possible output
8 cubed = 512 (execution time)
5 cubed = 125 (compilation time)