| 1 |
|
|---|
| 2 | #include <boost/assert.hpp>
|
|---|
| 3 |
|
|---|
| 4 | #include <boost/type_traits/remove_reference.hpp>
|
|---|
| 5 |
|
|---|
| 6 | #include <boost/utility/base_from_member.hpp>
|
|---|
| 7 |
|
|---|
| 8 | #include <algorithm>
|
|---|
| 9 | #include <vector>
|
|---|
| 10 |
|
|---|
| 11 | template< typename Iterator >
|
|---|
| 12 | struct iterator_something
|
|---|
| 13 | {
|
|---|
| 14 | iterator_something( Iterator const& first, Iterator const& last )
|
|---|
| 15 | : _first( first ), _last( last )
|
|---|
| 16 | {}
|
|---|
| 17 |
|
|---|
| 18 | std::size_t distance()
|
|---|
| 19 | {
|
|---|
| 20 | return std::distance( _first, _last );
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | Iterator _first, _last;
|
|---|
| 24 | };
|
|---|
| 25 |
|
|---|
| 26 | template< typename Container >
|
|---|
| 27 | struct container_something
|
|---|
| 28 | : boost::base_from_member< Container >
|
|---|
| 29 | , iterator_something<
|
|---|
| 30 | typename boost::remove_reference< Container >::type::iterator
|
|---|
| 31 | >
|
|---|
| 32 | {
|
|---|
| 33 | typedef boost::base_from_member< Container > container_member;
|
|---|
| 34 |
|
|---|
| 35 | explicit container_something( Container& container )
|
|---|
| 36 | : boost::base_from_member< Container >( container )
|
|---|
| 37 | , iterator_something<
|
|---|
| 38 | typename boost::remove_reference< Container >::type::iterator
|
|---|
| 39 | >( container_member::member.begin(), container_member::member.end() )
|
|---|
| 40 | {}
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | int main()
|
|---|
| 44 | {
|
|---|
| 45 | std::vector< int > v;
|
|---|
| 46 | v.push_back( 0 );
|
|---|
| 47 | v.push_back( 1 );
|
|---|
| 48 | v.push_back( 2 );
|
|---|
| 49 |
|
|---|
| 50 | container_something< std::vector< int > > something_copy( v );
|
|---|
| 51 | something_copy.distance(); // right
|
|---|
| 52 |
|
|---|
| 53 | container_something< std::vector< int >& > something_ref( v );
|
|---|
| 54 | something_ref.distance(); // wrong
|
|---|
| 55 | }
|
|---|