fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <bool isConstexpr>
  5. struct f_impl;
  6.  
  7. template <>
  8. struct f_impl<false>
  9. {
  10. static int f(int a)
  11. {
  12. cout << "well, I'm not constexpr f()\n";
  13. return a;
  14. }
  15. };
  16.  
  17. constexpr int f_constexpr(int a)
  18. {
  19. return a;
  20. }
  21.  
  22. template <>
  23. struct f_impl<true>
  24. {
  25. static constexpr int f(int a)
  26. {
  27. return f_constexpr(a);
  28. }
  29. };
  30.  
  31.  
  32. #define f(a) f_impl<noexcept(f_constexpr(a))>::f(a)
  33.  
  34. int main(int argc, char* argv[]) {
  35. constexpr int a = f(7);
  36. cout << "constexpr was called..." << endl;
  37. int c = f(argc);
  38. cout << "non constexpr was called, I guess..." << endl;
  39. cout << a << endl;
  40. cout << c << endl;
  41.  
  42. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
constexpr was called...
well, I'm not constexpr f()
non constexpr was called, I guess...
7
1