// (C) Copyright Alisdair Meredith 2007. // Use, modification and distribution are subject to the // Boost Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // See http://www.boost.org/libs/config for most recent version. // MACRO: BOOST_NO_IMPLICIT_BOOL_CONVERSION // TITLE: Implicit bool conversion // DESCRIPTION: The compliler should implicitly convert values // in specific contexts, such as the condition // inside an if/while clause, or when evaluating // logical operators && ||. // The convertible-to-unspecified-bool-type idiom // relies on this. Non-conforming compilers can // be helped out with explicit conversion to bool // in these contexts, preferably with static_cast namespace boost_no_implicit_bool_conversion { struct false_value { typedef int const false_value::* unspecified_bool; operator unspecified_bool() const { return unspecified_bool(); } }; struct true_value { typedef int (true_value::* unspecified_bool)(); operator unspecified_bool() const { return &true_value::hidden; } private: int hidden(); }; int test() { true_value True; false_value False; int count_fails = 0; if( False ) { ++count_fails; } if( !True ) { ++count_fails; } if( True && False ) { ++count_fails; } if( !(True || False) ) { ++count_fails; } count_fails += True ? 0 : 1; count_fails += False ? 1 : 0; while( false_value f = False ) {} do {} while( False ); for( ; False ; ) {} return count_fails; } }