Re: [Boost-bugs] [Boost C++ Libraries] #1295: for each in VC8+

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #1295: for each in VC8+
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2007-10-19 21:07:02


#1295: for each in VC8+
-------------------------------+--------------------------------------------
  Reporter: NN | Owner: eric_niebler
      Type: Feature Requests | Status: reopened
 Milestone: To Be Determined | Component: foreach
   Version: Boost 1.34.1 | Severity: Optimization
Resolution: | Keywords:
-------------------------------+--------------------------------------------
Changes (by NN):

  * status: closed => reopened
  * resolution: wontfix =>

Comment:

 I have found the way to make it work with all requirements:

 {{{
 #!cpp
 #include <boost/range/iterator_range.hpp>
 #include <boost/foreach.hpp>

 #if defined(_MSC_VER) && _MSC_VER >= 1400 // VC 8.0+
     #define foreach(var, col) \
         for each(var in (::boost::make_iterator_range(col)))
 #else // Other compilers
     #define foreach BOOST_FOREACH
 #endif // defined(_MSC_VER) && _MSC_VER >= 1400 // VC 8.0+
 }}}

 There is a code for test:
 {{{
 #!cpp

 #include <boost/range/begin.hpp>
 #include <boost/range/end.hpp>
 #include <boost/array.hpp>
 #include <boost/lambda/lambda.hpp>
 #include <vector>
 #include <cassert>
 #include <iostream>

 #include <iterator>

 namespace my
 {

 // Extensibility check
 struct non_iteratable
 {
     non_iteratable()
     {
         x[0] = 0;
         x[1] = 1;
     }

     typedef boost::array<int, 2> x_type;
     x_type x;
 };

 non_iteratable::x_type::iterator boost_range_begin(non_iteratable& c)
 {
     return boost::begin(c.x);
 }

 non_iteratable::x_type::iterator boost_range_end(non_iteratable& c)
 {
     return boost::end(c.x);
 }

 non_iteratable::x_type::const_iterator boost_range_begin(non_iteratable
 const& c)
 {
     return boost::begin(c.x);
 }

 non_iteratable::x_type::const_iterator boost_range_end(non_iteratable
 const& c)
 {
     return boost::end(c.x);
 }

 } // namespace my

 namespace boost
 {

 // specialize rannge_iterator and range_const_iterator in namespace boost
 template<>
 struct range_iterator<my::non_iteratable>
 {
     typedef my::non_iteratable::x_type::iterator type;
 };

 template<>
 struct range_const_iterator<my::non_iteratable>
 {
     typedef my::non_iteratable::x_type::const_iterator type;
 };

 } // namespace boost


 int main()
 {
     {
     // Regular array
     int a[10] = {};
     // Value
     foreach(int i, a)
         std::cout << i;

     // Reference to const
     foreach(int const& i, a)
         std::cout << i;

     // Reference to non Const
     foreach(int& i, a)
         i = 1;
     assert(std::find(boost::begin(a), boost::end(a), 0) == boost::end(a));
     }

     {
     // Boost.Array
     boost::array<int, 10> a = {};
     // Value
     foreach(int i, a)
         std::cout << i;

     // Reference to const
     foreach(int const& i, a)
         std::cout << i;

     // Reference to non Const
     foreach(int& i, a)
         i = 1;
     assert(std::find(boost::begin(a), boost::end(a), 0) == boost::end(a));
     }

     {
     // Std.Vector
     std::vector<int> a(10);
     // Value
     foreach(int i, a)
         std::cout << i;

     // Reference to const
     foreach(int const& i, a)
         std::cout << i;

     // Reference to non Const
     foreach(int& i, a)
         i = 1;
     assert(std::find(boost::begin(a), boost::end(a), 0) == boost::end(a));
     }

     // Extensibility
     {
     my::non_iteratable a;

     // Value
     foreach(int i, a)
         std::cout << i;

     // Reference to const
     foreach(int const& i, a)
         std::cout << i;

     // Reference to non Const
     foreach(int& i, a)
         i = 1;
     assert(std::find(boost::begin(a), boost::end(a), 0) == boost::end(a));
     }
 }
 }}}

 The code works as expected.
 Thank you for attention.

--
Ticket URL: <http://svn.boost.org/trac/boost/ticket/1295#comment:3>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.


This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:56 UTC