decay_test.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // (C) Copyright John Maddock & Thorsten Ottosen 2005.
  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. #ifdef TEST_STD
  6. # include <type_traits>
  7. #else
  8. # include <boost/type_traits/decay.hpp>
  9. # include <boost/type_traits/is_same.hpp>
  10. #endif
  11. #include "test.hpp"
  12. #include "check_type.hpp"
  13. #include "check_integral_constant.hpp"
  14. #include <iostream>
  15. #include <string>
  16. #include <utility>
  17. #ifdef BOOST_INTEL
  18. // remark #383: value copied to temporary, reference to temporary used
  19. // std::pair<std::string, int> p2 = boost::make_pair( "foo", 1 );
  20. // ^
  21. #pragma warning(disable:383)
  22. #endif
  23. namespace boost
  24. {
  25. int proc1()
  26. {
  27. return 0;
  28. }
  29. int proc2(int c)
  30. {
  31. return c;
  32. }
  33. //
  34. // An almost optimal version of std::make_pair()
  35. //
  36. template< class F, class S >
  37. inline std::pair< BOOST_DEDUCED_TYPENAME tt::decay<const F>::type,
  38. BOOST_DEDUCED_TYPENAME tt::decay<const S>::type >
  39. make_pair( const F& f, const S& s )
  40. {
  41. return std::pair< BOOST_DEDUCED_TYPENAME tt::decay<const F>::type,
  42. BOOST_DEDUCED_TYPENAME tt::decay<const S>::type >( f, s );
  43. }
  44. /*
  45. This overload will mess up vc7.1
  46. template< class F, class S >
  47. inline std::pair< BOOST_DEDUCED_TYPENAME ::tt::decay<F>::type,
  48. BOOST_DEDUCED_TYPENAME ::tt::decay<S>::type >
  49. make_pair( F& f, S& s )
  50. {
  51. return std::pair< BOOST_DEDUCED_TYPENAME ::tt::decay<F>::type,
  52. BOOST_DEDUCED_TYPENAME ::tt::decay<S>::type >( f, s );
  53. }
  54. */
  55. }
  56. BOOST_DECL_TRANSFORM_TEST3(decay_test_1, ::tt::decay, const)
  57. BOOST_DECL_TRANSFORM_TEST3(decay_test_2, ::tt::decay, volatile)
  58. BOOST_DECL_TRANSFORM_TEST3(decay_test_3, ::tt::decay, const volatile)
  59. BOOST_DECL_TRANSFORM_TEST3(decay_test_4, ::tt::decay, const&)
  60. BOOST_DECL_TRANSFORM_TEST3(decay_test_5, ::tt::decay, volatile&)
  61. BOOST_DECL_TRANSFORM_TEST3(decay_test_6, ::tt::decay, const volatile&)
  62. BOOST_DECL_TRANSFORM_TEST(decay_test_7, ::tt::decay, const*, const*)
  63. BOOST_DECL_TRANSFORM_TEST(decay_test_8, ::tt::decay, [], *)
  64. BOOST_DECL_TRANSFORM_TEST(decay_test_9, ::tt::decay, [2], *)
  65. BOOST_DECL_TRANSFORM_TEST(decay_test_10, ::tt::decay, [2][3], (*)[3])
  66. BOOST_DECL_TRANSFORM_TEST(decay_test_11, ::tt::decay, const[], const*)
  67. BOOST_DECL_TRANSFORM_TEST(decay_test_12, ::tt::decay, const[2], const*)
  68. BOOST_DECL_TRANSFORM_TEST(decay_test_13, ::tt::decay, const[2][3], const(*)[3])
  69. BOOST_DECL_TRANSFORM_TEST(decay_test_14, ::tt::decay, (int), (*)(int))
  70. TT_TEST_BEGIN(decay)
  71. decay_test_1();
  72. decay_test_2();
  73. decay_test_3();
  74. decay_test_4();
  75. decay_test_5();
  76. decay_test_6();
  77. decay_test_7();
  78. decay_test_8();
  79. decay_test_9();
  80. decay_test_10();
  81. decay_test_11();
  82. decay_test_12();
  83. decay_test_13();
  84. decay_test_14();
  85. BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same<
  86. ::tt::decay<int>::type,int>::value),
  87. true );
  88. BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same<
  89. ::tt::decay<char[2]>::type,char*>::value),
  90. true );
  91. BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same<
  92. ::tt::decay<char[2][3]>::type,char(*)[3]>::value),
  93. true );
  94. BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same<
  95. ::tt::decay<const char[2]>::type,const char*>::value),
  96. true );
  97. BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same<
  98. ::tt::decay<wchar_t[2]>::type,wchar_t*>::value),
  99. true );
  100. BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same<
  101. ::tt::decay<const wchar_t[2]>::type,const wchar_t*>::value),
  102. true );
  103. BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same<
  104. ::tt::decay<const wchar_t[2]>::type,const wchar_t*>::value),
  105. true );
  106. typedef int f1_type(void);
  107. typedef int f2_type(int);
  108. BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same<
  109. ::tt::decay<f1_type>::type,int (*)(void)>::value),
  110. true );
  111. BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_same<
  112. ::tt::decay<f2_type>::type,int (*)(int)>::value),
  113. true );
  114. #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
  115. //
  116. // Don't test this if the std lib has no templated constructors (Oracle+STLPort):
  117. //
  118. std::pair<std::string,std::string> p = boost::make_pair( "foo", "bar" );
  119. std::pair<std::string, int> p2 = boost::make_pair( "foo", 1 );
  120. #ifndef BOOST_NO_STD_WSTRING
  121. std::pair<std::wstring,std::string> p3 = boost::make_pair( L"foo", "bar" );
  122. std::pair<std::wstring, int> p4 = boost::make_pair( L"foo", 1 );
  123. #endif
  124. #endif
  125. //
  126. // Todo: make these work sometime. The test id not directly
  127. // related to decay<T>::type and can be avoided for now.
  128. //
  129. /*
  130. int array[10];
  131. std::pair<int*,int*> p5 = boost::make_pair( array, array );
  132. #ifndef __BORLANDC__
  133. std::pair<int(*)(void), int(*)(int)> p6 = boost::make_pair(boost::proc1, boost::proc2);
  134. p6.first();
  135. p6.second(1);
  136. #endif
  137. */
  138. TT_TEST_END