1 #include <boost/config.hpp>
2 
3 #if defined(BOOST_MSVC)
4 #pragma warning(disable: 4786)  // identifier truncated in debug info
5 #pragma warning(disable: 4710)  // function not inlined
6 #pragma warning(disable: 4711)  // function selected for automatic inline expansion
7 #pragma warning(disable: 4514)  // unreferenced inline removed
8 #endif
9 
10 //
11 //  bind_and_or_test.cpp - &&, || operators
12 //
13 //  Copyright (c) 2008 Peter Dimov
14 //
15 // Distributed under the Boost Software License, Version 1.0. (See
16 // accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
18 //
19 
20 #include <boost/bind.hpp>
21 
22 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
23 #pragma warning(push, 3)
24 #endif
25 
26 #include <iostream>
27 
28 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
29 #pragma warning(pop)
30 #endif
31 
32 #include <boost/detail/lightweight_test.hpp>
33 
f(bool x)34 bool f( bool x )
35 {
36     return x;
37 }
38 
g(bool x)39 bool g( bool x )
40 {
41     return !x;
42 }
43 
h()44 bool h()
45 {
46     BOOST_ERROR( "Short-circuit evaluation failure" );
47     return false;
48 }
49 
test(F f,A1 a1,A2 a2,R r)50 template< class F, class A1, class A2, class R > void test( F f, A1 a1, A2 a2, R r )
51 {
52     BOOST_TEST( f( a1, a2 ) == r );
53 }
54 
main()55 int main()
56 {
57     // &&
58 
59     test( boost::bind( f, true ) && boost::bind( g, true ), false, false, f( true ) && g( true ) );
60     test( boost::bind( f, true ) && boost::bind( g, false ), false, false, f( true ) && g( false ) );
61 
62     test( boost::bind( f, false ) && boost::bind( h ), false, false, f( false ) && h() );
63 
64     test( boost::bind( f, _1 ) && boost::bind( g, _2 ), true, true, f( true ) && g( true ) );
65     test( boost::bind( f, _1 ) && boost::bind( g, _2 ), true, false, f( true ) && g( false ) );
66 
67     test( boost::bind( f, _1 ) && boost::bind( h ), false, false, f( false ) && h() );
68 
69     // ||
70 
71     test( boost::bind( f, false ) || boost::bind( g, true ), false, false, f( false ) || g( true ) );
72     test( boost::bind( f, false ) || boost::bind( g, false ), false, false, f( false ) || g( false ) );
73 
74     test( boost::bind( f, true ) || boost::bind( h ), false, false, f( true ) || h() );
75 
76     test( boost::bind( f, _1 ) || boost::bind( g, _2 ), false, true, f( false ) || g( true ) );
77     test( boost::bind( f, _1 ) || boost::bind( g, _2 ), false, false, f( false ) || g( false ) );
78 
79     test( boost::bind( f, _1 ) || boost::bind( h ), true, false, f( true ) || h() );
80 
81     //
82 
83     return boost::report_errors();
84 }
85