bind_dm_test.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*==============================================================================
  2. Copyright (c) 2005 Peter Dimov
  3. Copyright (c) 2005-2010 Joel de Guzman
  4. Copyright (c) 2010 Thomas Heller
  5. Copyright (c) 2015 John Fletcher
  6. Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. ==============================================================================*/
  9. #include <boost/config.hpp>
  10. #if defined(BOOST_MSVC)
  11. #pragma warning(disable: 4786) // identifier truncated in debug info
  12. #pragma warning(disable: 4710) // function not inlined
  13. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  14. #pragma warning(disable: 4514) // unreferenced inline removed
  15. #endif
  16. #include <boost/phoenix/core.hpp>
  17. #include <boost/phoenix/bind.hpp>
  18. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  19. #pragma warning(push, 3)
  20. #endif
  21. #include <iostream>
  22. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  23. #pragma warning(pop)
  24. #endif
  25. #include <boost/detail/lightweight_test.hpp>
  26. struct X
  27. {
  28. int m;
  29. };
  30. X f( int v )
  31. {
  32. X r = { v };
  33. return r;
  34. }
  35. int main()
  36. {
  37. using boost::phoenix::bind;
  38. using boost::phoenix::ref;
  39. using boost::phoenix::placeholders::_1;
  40. X x = { 17041 };
  41. X * px = &x;
  42. BOOST_TEST( bind( &X::m, _1 )( x ) == 17041 );
  43. BOOST_TEST( bind( &X::m, _1 )( px ) == 17041 );
  44. BOOST_TEST( bind( &X::m, x )() == 17041 );
  45. BOOST_TEST( bind( &X::m, px )() == 17041 );
  46. BOOST_TEST( bind( &X::m, ref(x) )() == 17041 );
  47. X const cx = x;
  48. X const * pcx = &cx;
  49. BOOST_TEST( bind( &X::m, _1 )( cx ) == 17041 );
  50. BOOST_TEST( bind( &X::m, _1 )( pcx ) == 17041 );
  51. BOOST_TEST( bind( &X::m, cx )() == 17041 );
  52. BOOST_TEST( bind( &X::m, pcx )() == 17041 );
  53. BOOST_TEST( bind( &X::m, ref(cx) )() == 17041 );
  54. int const v = 42;
  55. // NOTE: The second case does not work with compiler optimization.
  56. // This is a bug which has not yet been fixed.
  57. // The current test for gcc 4.7.3 does use -O2 but does not
  58. // satisfy this first part of the test for some unknown reason.
  59. // So this is set to run the first case for all gcc 4.7
  60. #if (defined(__OPTIMIZE__) && __OPTIMIZE__) || \
  61. defined(BOOST_GCC_VERSION) && (BOOST_GCC_VERSION >= 40700) && (BOOST_GCC_VERSION < 40800)
  62. // Change bind_dm_test.cpp to bind to _1 twice.
  63. BOOST_TEST( bind( &X::m, _1)( bind( f, _1 )( v ) ) == v );
  64. #else
  65. BOOST_TEST( bind( &X::m, bind( f, _1 ) )( v ) == v );
  66. #endif
  67. return boost::report_errors();
  68. }