12 ms·
For integral values you can use a little hack to ensure compile-time evaluation: namespace detail { template<class T, T val> constexpr T ev
by nightcracker 9y ago
For integral values you can use a little hack to ensure compile-time evaluation:
namespace detail {
template<class T, T val>
constexpr T eval() { return val; }
};
#define FORCE_COMPILE_TIME(x) detail::eval<decltype(x), x>()
- beeforpork 9y agoThis is exactly why I don't like C++: it is just too complicated, and it encourages to write complicated code, usually opaque boilerplate template hacks for what would seem a simple concept. This is difficult to read, review, understand, and hence error-prone. No, not only error-prone, but bug-encouraging. BTW, you can also enforce compile time evaluation like this: enum { aux1 = X }; use(aux1); But that's two lines, I know. And, of course, it is still a hack ('enum'? What?).
- ben-schaaf 9y agoInterestingly D actually used `enum` to define compile time values (as opposed to `alias` which is used for types). If you want a compile time value you can do this: enum aux1 = X;