non_copyable.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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/eval_if.hpp>
  6. #include <boost/hana/fwd/not.hpp>
  7. #include <boost/hana/fwd/while.hpp>
  8. #include <boost/hana/if.hpp>
  9. namespace hana = boost::hana;
  10. // This test makes sure that if_ can be used with non-copyable branches.
  11. template <bool Value>
  12. struct Boolean { };
  13. namespace boost { namespace hana {
  14. template <bool Value>
  15. struct while_impl<Boolean<Value>> {
  16. // Not implemented
  17. };
  18. template <bool Value>
  19. struct not_impl<Boolean<Value>> {
  20. // Not implemented
  21. };
  22. template <bool Value>
  23. struct eval_if_impl<Boolean<Value>> {
  24. template <typename Cond, typename Then, typename Else>
  25. static constexpr decltype(auto) apply(Cond const&, Then&& t, Else&& e) {
  26. return hana::eval_if(hana::bool_c<Value>, static_cast<Then&&>(t),
  27. static_cast<Else&&>(e));
  28. }
  29. };
  30. }}
  31. template <int v>
  32. struct NonCopyable {
  33. static constexpr int value = v;
  34. NonCopyable() = default;
  35. NonCopyable(NonCopyable const&) = delete;
  36. NonCopyable(NonCopyable&&) = default;
  37. NonCopyable& operator=(NonCopyable const&) = delete;
  38. NonCopyable& operator=(NonCopyable&&) = default;
  39. };
  40. static_assert(hana::if_(Boolean<true>{}, NonCopyable<3>{}, NonCopyable<4>{}).value == 3, "");
  41. static_assert(hana::if_(Boolean<false>{}, NonCopyable<3>{}, NonCopyable<4>{}).value == 4, "");
  42. int main() { }