boost_no_constexpr.ipp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // (C) Copyright Beman Dawes 2008
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/config for more information.
  6. // MACRO: BOOST_NO_CXX11_CONSTEXPR
  7. // TITLE: C++0x constexpr unavailable
  8. // DESCRIPTION: The compiler does not support C++0x constexpr
  9. namespace boost_no_cxx11_constexpr {
  10. void quiet_warning(int){}
  11. constexpr int square(int x) { return x * x; } // from N2235
  12. // from 5.19:
  13. constexpr const int* addr(const int& ir) { return &ir; }
  14. static const int x = 5;
  15. constexpr const int* xp = addr(x);
  16. struct A
  17. {
  18. constexpr A(int i) : val(i) { }
  19. constexpr operator int()const { return val; }
  20. constexpr operator long()const { return 43; }
  21. private:
  22. int val;
  23. };
  24. template<int> struct X { };
  25. constexpr const A a = 42;
  26. X<a> xx; // OK: unique conversion to int
  27. // virtual function
  28. struct B
  29. {
  30. virtual void vf() {}
  31. };
  32. struct C : B
  33. {
  34. constexpr C() {}
  35. };
  36. // aggregate initialization
  37. struct D
  38. {
  39. int val[2];
  40. constexpr D() : val() {}
  41. };
  42. // virtual base
  43. struct E
  44. {
  45. };
  46. struct F : virtual E
  47. {
  48. };
  49. constexpr F& f(F& out) { return out; }
  50. namespace whatever{};
  51. constexpr int factorial(int i)
  52. {
  53. typedef int value_type;
  54. using namespace whatever;
  55. return i <= 1 ? 1 : i * factorial(value_type(i-1));
  56. }
  57. int test()
  58. {
  59. constexpr int i = square(5) + factorial(10);
  60. quiet_warning(i);
  61. switch (i)
  62. {
  63. case a:
  64. break;
  65. }
  66. return 0;
  67. }
  68. }