ein*_*ica 5 c++ constexpr c++17
我正在实现一个constexpr int foo();功能。在 的主体中foo(),我想在编译时做一些不同的事情(并且可能返回一些不同的东西),而在运行时做一些不同的事情。
使用 C++20,我可以使用std::is_constant_evaluated:
constexpr int foo() { return std::is_constant_evaluated() ? 123 : 456 };
Run Code Online (Sandbox Code Playgroud)
但是如果我使用的是 C++17(或更早版本)怎么办 - 我可以用同样的效果做什么?
注意:特定于编译器的解决方案是可以接受的(尽管不太理想)。