#ifndef TESTS_RECURSE_HPP #define TESTS_RECURSE_HPP #include #include #include // // This file instantiates a recursive template, with compile-time // assertions that fail if recursion reaches some depth. // // To use, include this header from somewhere // // Configuration: // // This will assert if recursion gets deeper than the constants // "class_max_depth" and "function_max_depth" below. Change them // to assert at some specific depth. // // "do_recurse" at the end of this file instantiates the templates. // Change it to instantiate another depth. // namespace tests { int const class_max_depth = 5, //assert at class scope at this detpth function_max_depth = 4; //assert at member-function scope at this depth template struct type_assert {}; template struct assertion : boost::mpl::true_ { enum { assert }; }; template<> struct assertion : boost::mpl::false_ {}; template struct too_deep_in_class : assertion {}; template struct too_deep_in_function : assertion {}; template struct depth { enum depth_e { result = depth_ }; // assert at class scope BOOST_STATIC_ASSERT(( too_deep_in_class::value )); BOOST_MPL_ASSERT(( too_deep_in_class )); typedef type_assert< too_deep_in_class > assert; }; template struct recurse { template int function(depth const&) { // assert at member function scope BOOST_STATIC_ASSERT(( too_deep_in_function::value )); BOOST_MPL_ASSERT(( too_deep_in_function )); typedef type_assert< too_deep_in_function > assert; // recurse return next_().function(depth()); } operator int() const { return recurse().function(depth<1>()); } }; template<> struct recurse { template int function(depth const&) { // assert at member function scope BOOST_STATIC_ASSERT(( too_deep_in_function::value )); BOOST_MPL_ASSERT(( too_deep_in_function )); typedef type_assert< too_deep_in_function > assert; // dont recurse return 0; } operator int() const { return recurse().function(depth<1>()); } }; namespace { //uncomment below to go deeper int do_recurse = recurse > > > > //25 //> > > > > //20 //> > > > > //15 //> > > > > //10 > > > > >(); //5 } } //namespace tests #endif //TESTS_RECURSE_HPP