summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/foreach/test/noncopyable.cpp
blob: 60a2eebd2bf29ee13bd90a3590056cf89500453c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//  noncopyable.cpp
///
//  (C) Copyright Eric Niebler 2004.
//  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)

/*
 Revision history:
   21 December 2005 : Initial version.
*/

#include <vector>
#include <boost/foreach.hpp>
#include <boost/noncopyable.hpp>
#include <boost/range/iterator_range.hpp>

struct noncopy_vector
  : std::vector<int>
  , private boost::noncopyable
{
  noncopy_vector() { }
};

struct noncopy_range
  : boost::iterator_range<noncopy_vector::iterator>
  , private boost::noncopyable
{
  noncopy_range() { }
};

// Tell FOREACH that noncopy_vector and noncopy_range are non-copyable.
// NOTE: this is only necessary if
//   a) your type does not inherit from boost::noncopyable, OR
//   b) Boost.Config defines BOOST_BROKEN_IS_BASE_AND_DERIVED for your compiler
#ifdef BOOST_BROKEN_IS_BASE_AND_DERIVED
inline boost::mpl::true_ *boost_foreach_is_noncopyable(noncopy_vector *&, boost::foreach::tag)
{
    return 0;
}

inline boost::mpl::true_ *boost_foreach_is_noncopyable(noncopy_range *&, boost::foreach::tag)
{
    return 0;
}
#endif

// tell FOREACH that noncopy_range is a lightweight proxy object
inline boost::mpl::true_ *boost_foreach_is_lightweight_proxy(noncopy_range *&, boost::foreach::tag)
{
    return 0;
}

///////////////////////////////////////////////////////////////////////////////
// main
//   
int main( int, char*[] )
{
    noncopy_vector v1;
    BOOST_FOREACH( int & i, v1 ) { (void)i; }

    noncopy_vector const v2;
    BOOST_FOREACH( int const & j, v2 ) { (void)j; }

    noncopy_range rng1;
    BOOST_FOREACH( int & k, rng1 ) { (void)k; }

    noncopy_range const rng2;
    BOOST_FOREACH( int & l, rng2 ) { (void)l; }

    return 0;
}