I was watching Bjarne Stroustrup's talk "The Essential C++".
In Q&A session, on how to manage heavy template programming codes, he mentioned that: "by constack perfunction you can eliminate essentially every template metaprogramming that generates a value by writting ordinary code".
The constack perfunction is just a wild guess by the sound.
May I ask what is the correct term for that technology? so that I could do some follow up reading.
update: just modify the title to "constexpr function".
constexpr
functions, added in C++11, can be evaluated at compile-time and be used as template arguments in template metaprogramming. In C++11 they are very limited and can (nearly) only consist of a single return
expression. C++14 makes them less restrictive.
For example this is possible:
constexpr std::size_t twice(std::size_t sz) {
return 2 * sz;
}
std::array<int, twice(5)> array;
Whereas before C++11, template 'hacks' were needed, like for instance:
template<std::size_t sz>
class twice {
public:
static const std::size_t value = 2 * sz;
}
std::array<int, twice<5>::value> array;
It can for instance be used to generate values (like math constants, trigonometric lookup tables, ...) at compile-time in a clean manner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With