tuple_apply_cx.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. constexpr int f( int x, int y, int z )
  20. {
  21. return x * 100 + y * 10 + z;
  22. }
  23. constexpr int g( int x, int y )
  24. {
  25. return x * 10 + y;
  26. }
  27. constexpr int h()
  28. {
  29. return 11;
  30. }
  31. int main()
  32. {
  33. {
  34. constexpr std::tuple<int, short, char> tp{ 1, 2, 3 };
  35. constexpr auto r = boost::mp11::tuple_apply( f, tp );
  36. static_assert( r == 123, "r == 123" );
  37. }
  38. {
  39. constexpr std::pair<short, char> tp{ 1, 2 };
  40. constexpr auto r = boost::mp11::tuple_apply( g, tp );
  41. static_assert( r == 12, "r == 12" );
  42. }
  43. {
  44. constexpr std::array<short, 3> tp{{ 1, 2, 3 }};
  45. constexpr auto r = boost::mp11::tuple_apply( f, tp );
  46. static_assert( r == 123, "r == 123" );
  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 = boost::mp11::tuple_apply( h, tp );
  54. static_assert( r == 11, "r == 11" );
  55. }
  56. #endif
  57. }
  58. #endif