constexpr_init.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/bool.hpp>
  5. #include <boost/hana/integral_constant.hpp>
  6. namespace hana = boost::hana;
  7. /*
  8. When we use `int_<...>` in a template, Clang 3.5 says:
  9. --------------------------------
  10. include/boost/hana/integral_constant.hpp:80:20: error: constexpr variable 'int_<1>' must be initialized by a constant expression
  11. constexpr auto int_ = integral<int, i>;
  12. ^ ~~~~~~~~~~~~~~~~
  13. test/integral/constexpr_bug.cpp:41:37: note: in instantiation of variable template specialization 'boost::hana::int_' requested here
  14. constexpr auto check_int() { return int_<1>; }
  15. ^
  16. include/boost/hana/integral_constant.hpp:80:27: note: subexpression not valid in a constant expression
  17. constexpr auto int_ = integral<int, i>;
  18. ^
  19. include/boost/hana/integral_constant.hpp:80:27: note: in call to 'integral_type(integral)'
  20. --------------------------------
  21. if we define int_ & friends like
  22. template <int i>
  23. constexpr auto int_ = integral<int, i>;
  24. Instead, we do
  25. template <int i>
  26. constexpr decltype(integral<int, i>) int_{};
  27. which is equivalent but uglier. Note that everything works just fine when
  28. we're not in a template.
  29. */
  30. template <typename T>
  31. constexpr auto check_int() { return hana::int_c<1>; }
  32. template <typename T>
  33. constexpr auto check_true() { return hana::true_c; }
  34. template <typename T>
  35. constexpr auto check_size_t() { return hana::size_c<0>; }
  36. int main() { }