4 ms·
You can get virtually the same effect with marking functions `constexpr` in C++. Yes, you need to do it explicitly, and yes, you can't call functions that aren'
by MauranKilom 5y ago
You can get virtually the same effect with marking functions `constexpr` in C++. Yes, you need to do it explicitly, and yes, you can't call functions that aren't marked `constexpr` inside, but it's more or less the same - just opt-in.
In fact, since C++20, you don't need any template metaprogramming for compile-time sorting - std::sort is constexpr! [0]
Demo: https://godbolt.org/z/6M1Wqc31W https://godbolt.org/z/6M1Wqc31W
[0]: https://en.cppreference.com/w/cpp/algorithm/sort https://en.cppreference.com/w/cpp/algorithm/sort
- cyber_kinetist 5y agoI’m worried about the compile-time performance though. C++ compilers currently use direct tree-walking on the AST to interpret code, which is going to be horrendously slow once you go past a certain number of elements.
- csb6 5y agoI’m not sure what GCC and MSVC are doing, but Clang [1] at least is in the process of developing a bytecode interpreter for running constexpr code more efficiently. It seems like it hasn’t been actively worked on in quite some time, unfortunately, based on the commit logs. [1] https://clang.llvm.org/docs/ConstantInterpreter.html https://clang.llvm.org/docs/ConstantInterpreter.html