1 // noncopyable.cpp
2 ///
3 // (C) Copyright Eric Niebler 2004.
4 // Use, modification and distribution are subject to the
5 // Boost Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 /*
9 Revision history:
10 21 December 2005 : Initial version.
11 */
12
13 #include <vector>
14 #include <boost/foreach.hpp>
15 #include <boost/noncopyable.hpp>
16 #include <boost/range/iterator_range.hpp>
17
18 struct noncopy_vector
19 : std::vector<int>
20 , private boost::noncopyable
21 {
noncopy_vectornoncopy_vector22 noncopy_vector() { }
23 };
24
25 struct noncopy_range
26 : boost::iterator_range<noncopy_vector::iterator>
27 , private boost::noncopyable
28 {
noncopy_rangenoncopy_range29 noncopy_range() { }
30 };
31
32 // Tell FOREACH that noncopy_vector and noncopy_range are non-copyable.
33 // NOTE: this is only necessary if
34 // a) your type does not inherit from boost::noncopyable, OR
35 // b) Boost.Config defines BOOST_BROKEN_IS_BASE_AND_DERIVED for your compiler
36 #ifdef BOOST_BROKEN_IS_BASE_AND_DERIVED
boost_foreach_is_noncopyable(noncopy_vector * &,boost::foreach::tag)37 inline boost::mpl::true_ *boost_foreach_is_noncopyable(noncopy_vector *&, boost::foreach::tag)
38 {
39 return 0;
40 }
41
boost_foreach_is_noncopyable(noncopy_range * &,boost::foreach::tag)42 inline boost::mpl::true_ *boost_foreach_is_noncopyable(noncopy_range *&, boost::foreach::tag)
43 {
44 return 0;
45 }
46 #endif
47
48 // tell FOREACH that noncopy_range is a lightweight proxy object
boost_foreach_is_lightweight_proxy(noncopy_range * &,boost::foreach::tag)49 inline boost::mpl::true_ *boost_foreach_is_lightweight_proxy(noncopy_range *&, boost::foreach::tag)
50 {
51 return 0;
52 }
53
54 ///////////////////////////////////////////////////////////////////////////////
55 // main
56 //
main(int,char * [])57 int main( int, char*[] )
58 {
59 noncopy_vector v1;
60 BOOST_FOREACH( int & i, v1 ) { (void)i; }
61
62 noncopy_vector const v2;
63 BOOST_FOREACH( int const & j, v2 ) { (void)j; }
64
65 noncopy_range rng1;
66 BOOST_FOREACH( int & k, rng1 ) { (void)k; }
67
68 noncopy_range const rng2;
69 BOOST_FOREACH( int & l, rng2 ) { (void)l; }
70
71 return 0;
72 }
73