construct_from_tuple_cx.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2015 Peter Dimov.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. //
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #if defined(_MSC_VER)
  8. #pragma warning( disable: 4244 ) // 'initializing': conversion from 'int' to 'char', possible loss of data
  9. #endif
  10. #include <boost/mp11/tuple.hpp>
  11. #include <boost/mp11/detail/config.hpp>
  12. // Technically std::tuple isn't constexpr enabled in C++11, but it works with libstdc++
  13. #if defined( BOOST_MP11_NO_CONSTEXPR ) || ( !defined( __GLIBCXX__ ) && __cplusplus < 201400L )
  14. int main() {}
  15. #else
  16. #include <tuple>
  17. #include <array>
  18. #include <utility>
  19. struct T1
  20. {
  21. int x, y, z;
  22. constexpr T1( int x = 0, int y = 0, int z = 0 ): x(x), y(y), z(z) {}
  23. };
  24. int main()
  25. {
  26. using boost::mp11::construct_from_tuple;
  27. {
  28. constexpr std::tuple<int, short, char> tp{ 1, 2, 3 };
  29. constexpr auto r = construct_from_tuple<T1>( tp );
  30. static_assert( r.x == 1, "r.x == 1" );
  31. static_assert( r.y == 2, "r.y == 2" );
  32. static_assert( r.z == 3, "r.z == 3" );
  33. }
  34. {
  35. constexpr std::pair<short, char> tp{ 1, 2 };
  36. constexpr auto r = construct_from_tuple<T1>( tp );
  37. static_assert( r.x == 1, "r.x == 1" );
  38. static_assert( r.y == 2, "r.y == 2" );
  39. static_assert( r.z == 0, "r.z == 0" );
  40. }
  41. {
  42. constexpr std::array<short, 3> tp{{ 1, 2, 3 }};
  43. constexpr auto r = construct_from_tuple<T1>( tp );
  44. static_assert( r.x == 1, "r.x == 1" );
  45. static_assert( r.y == 2, "r.y == 2" );
  46. static_assert( r.z == 3, "r.z == 3" );
  47. }
  48. #if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 9
  49. // "error: default initialization of an object of const type 'const std::tuple<>' without a user-provided default constructor"
  50. #else
  51. {
  52. constexpr std::tuple<> tp;
  53. constexpr auto r = construct_from_tuple<T1>( tp );
  54. static_assert( r.x == 0, "r.x == 0" );
  55. static_assert( r.y == 0, "r.y == 0" );
  56. static_assert( r.z == 0, "r.z == 0" );
  57. }
  58. #endif
  59. }
  60. #endif