Showing posts with label Comment on Reader Q&A: auto and for loop index variables by Gene Bushuyev. Show all posts
Showing posts with label Comment on Reader Q&A: auto and for loop index variables by Gene Bushuyev. Show all posts

Wednesday, January 21, 2015

Comment on Reader Q&A: auto and for loop index variables by Gene Bushuyev

 

Comment on Reader Q&A: auto and for loop index variables by Joe

It might be overkill, but one option is to define an inline function that returns a 0 of the correct type for the container.
  template <class C>  auto constexpr zero(const C &c) -> decltype(size(c)) {      return 0;  }  
Then user code could look like
  for (auto i = zero(myContainer); i < size(myContainer); ++i) {     /// Do whatever  }  
An possible alternative name for this function is firstIndex.

Read More »

Comment on Reader Q&A: auto and for loop index variables by Simon Ferquel

One thing worth noting also, is that the for-range-loop construct does not require real iterators (ie: it does not check any iterator traits, and only use a very small part of the public signature of a full-fledged iterator).
For a type to work with the for range loop, you only need to have a
begin(MyType&)
and
end(MyType&)
returning an object supporting the following members:
  T operator*()const; // return the value currently pointed to by the iterator  void operator++(); // move the iterator to the next value  bool operator!=(const TIterator&) const; // compare the iterator to another one (used to check if we reached the end of the loop  
So it is really easy to extend existing components to support a for-range-loop construct.

Read More »

Comment on Reader Q&A: auto and for loop index variables by Steve

How about
      std::vector< int > v { 0, 1, 2, 3, 4 };        for ( auto&& t = v.size(), i = 0*t; i < t; ++i )      {          std::cout << i << "\n";      }  

Read More »

Comment on Reader Q&A: auto and for loop index variables by Gene Bushuyev

Ranges is a good idea in general, but they wouldn’t resolve this issue, they would just provide a superficially different syntax.
The issue here is the type conversion, and it’s int 0 that should be converted to the size_type, not the other argument to int. And unless we decide on some construct that always casts the first argument to the second argument type (might not be a great idea) we have to specify the type explicitly. Thus, the obvious solutions are still the most readable and correct ones:
  for(size_type i = 0, size = someObject.size(); i < size; ++i)  ...  for(auto i = (size_type)0, size = someObject.size(); i < size; ++i)  ...  
And if size_type is something frequently found in the code, then creating a user-defined literal will make it shorter, and easier to read:
  size_type operator"" _sz(unsigned long long i) { return size_type(i); }  for(auto i = 0_sz, size = someObject.size(); i < size; ++i)  ...  
Also, if 0 is frequently converted to size_type, it makes sense to define a constant of that type:
  const size_type zero = (size_type)0;  for(auto i = zero, size = someObject.size(); i < size; ++i)  ...  

Read More »