3

Basically, I need to check if a function call can be resolved in compile-time or not and based on that, generate one constexpr function for compile-time use and one non-constexpr function for run-time use. Is it even possible in C++14?

Example

I want to use (probably extensively optimized) std::abs() for run-time calls but, if possible, I want to use my own constexpr implementation of abs().


I know that g++ already implements a lot of functions as constexpr, but I want clang++ compatibility as well. Not to mention, this is not limited to abs, I would possibly use it for other cases as well.

sjaustirni
  • 3,056
  • 7
  • 32
  • 50
  • Do you want to know if some function is constexpr or not? – Ilya Popov Mar 09 '16 at 16:25
  • As I recal Johannes Schaub's trick was based on `noexcept` semantics. In the end I chose not to use that and just made the `constexpr` function not unreasonably inefficient (O(log n) stack usage, I saw no way to completely fix it). Note: **"clever" solutions are brittle**, generally. – Cheers and hth. - Alf Mar 09 '16 at 16:29
  • The compiler will do its best to resolve at compile time, and the very same `constexpr` function will be compiled if needed. Do you want the compiler to tell you what it decided to do for a particular call? Or do you want to override the std::abs() call? – wally Mar 09 '16 at 16:30
  • 1
    Here is another [trick](http://coliru.stacked-crooked.com/a/24785fe2f3eb6798) (from [Filip Roséen - refp](http://stackoverflow.com/users/1090079/filip-ros%C3%A9en-refp)): [Determine `constexpr` execution - during compilation or at runtime?](http://stackoverflow.com/questions/28683234/determine-constexpr-execution-during-compilation-or-at-runtime#comment45664014_28683234) – nonsensation Mar 09 '16 at 16:50

0 Answers0