What is the equivalent of C ++ 98 for referencing an automatic iterator?

In my development environment there is RHEL 5.8, which does not support GCC 4.8+ compilers (C ++ 11 +). I expect that someday we will get there, so I have a header file where I define macros based on C ++ 11 support levels, so I can do something like this:

#if defined(CPP11_auto_type_inference) && defined(CPP11_range_based_for_loops)
  for (auto vit : args)
#else
  std::vector<std::string>::const_iterator vit, vend;
  for (vend=args.end(),vit=args.begin(); vit != vend; ++vit)
#endif
  { // process arguments...
    std::cout << "Processing \"" << *vit << '"' << std::endl;
    . . .
  } // end "process arguments" loop

So, what I'm trying to do in C ++ 98 is equivalent to the reference iterator (or more precisely say "dereferenced iterator"?), As shown below:

for (auto& it : args)
  std::cout << "Processing \"" << it << '"' << std::endl;

Throughout life, I cannot figure out how to get a dereferenced iterator (or iterator reference) in C ++ 98. I can mimic it, as shown below:

#if defined(CPP11_auto_type_inference) && defined(CPP11_range_based_for_loops)
  for (auto& it : args) {
#else
  std::vector<std::string>::const_iterator vit, vend;
  for (vend=args.end(),vit=args.begin(); vit != vend; ++vit) {
    std::string it(*vit);
#endif
    std::cout << "Processing \"" << it << '"' << std::endl;
    . . .
  }

... but I really hope this is not the answer.

++ 98 for (auto& it : vec), ? "" , ?

, " " ++ 11 auto&? ( , ). , for (auto& it : vec) , for (auto it : vec)?

.

+1
2

, , ++ 11. ++ 98/03. , ++ 11.

, :

#if defined(CPP11_auto_type_inference) && defined(CPP11_range_based_for_loops)
  for (auto vit : args)
#else
  std::vector<std::string>::const_iterator viter, vend;
  for (vend=args.end(),viter=args.begin(); viter != vend; ++viter)
  {
     std::string vit = *viter;
#endif
     { // process arguments...
       std::cout << "Processing \"" << vit << '"' << std::endl;
       . . .
     } // end "process arguments" loop
#if defined(CPP11_auto_type_inference) && defined(CPP11_range_based_for_loops)  
#else
  }
#endif

  for (auto& vit : args)

:

     std::string const& vit = *viter;
+3

++ 98, Niebler : FOREACH Redux

"", , , "auto_iterator".

struct auto_any_base {};

template< class T > struct auto_any :
auto_any_base
{
   auto_any( T const & t ) : item( t ) {}
   mutable T item;
};

template< class Container >
auto_any< typename Container::const_iterator >
begin( Container const & c )
{
   return c.begin();
}

"auto_any", (.. ).

, ( ) FOREACH :

#define BOOST_FOREACH( item, container ) \
   auto_any_base const & iter = begin( container ); \ 

( , , looooot , )

+2

Source: https://habr.com/ru/post/1628041/


All Articles