No change loop variable in the loop?

Is there a way not to “use” the loop variable in a range-based loop, but also to avoid warning the compiler that it is not in use?

In context, I am trying to do something like the following. I have “handle warnings as errors,” and I would rather not hack how to make the variable “use,” meaninglessly mentioning it somewhere.

size_t getSize(const std::forward_list &list) { size_t count = 0; for (auto & : list) // compile error, but if i do "auto &i" here, MSVC // complains (reasonably) that i is unused { ++count; } return count; } 

I know there are other ways to do this, but let me say, for the sake of argument, that I need to use a range-based loop.

+8
c ++ foreach c ++ 11
Feb 15 '14 at 1:38
source share
4 answers

You can always explicitly indicate that a variable is not used in the body of the loop:

 ptrdiff_t size( std::forward_list const& list ) { ptrdiff_t count = 0; for( auto& dummy : list ) { (void) dummy; struct dummy; // Wrap this in a macro if you want. // Here everybody including compiler knows that dummy isn't used and can't be used. ++count; } return count; } 

The above, however, is much less clear than just using regular for -loop.

Not to mention just calling size .

+5
Feb 15 '14 at 3:59
source share

I think for this reason use std :: for_each , for example:

 template<typename T> std::size_t get_size(std::forward_list<T> const& list) { std::size_t count = 0; std::for_each(begin(list), end(list), [&count](T const& ){++count;} ); return count; } 

But if you get the size of any container, use std :: distance

  std::size_t count = std::distance(begin(list), end(list) ); 
+4
Feb 15 '14 at 4:23
source share

You can define a macro:

 #if defined(__GNUC__) # define UNUSED __attribute__ ((unused)) #elif defined(_MSC_VER) # define UNUSED __pragma(warning(suppress:4100)) #else # define UNUSED #endif ... for (auto &dummy UNUSED : list) { ++count; } ... 

It works great with GCC and CLANG (not so sure about MSVC ... I seem to remember that MSVC will disable the warning for the rest of the file).

also:

 template<class T> void unused(const T &) {} ... for (auto &dummy : list) { unused(dummy); ++count; } ... 

works with all compilers and should not have any overhead ( Mailbag: turning off compiler warnings ).

The Boost header <boost/core/ignore_unused.hpp> (Boost> = 1.56) for the same purpose defines the template for the boost::ignore_unused() function.

With C ++ 11, also std::ignore is a good choice:

 { std::ignore = dummy; // ... } 

Related questions:

  • C ++ 11 for a loop: how to ignore a value?
  • C ++ 11 for loops without loop variable



PS C ++ 17 seems to get the [[maybe_unused]] attribute to provide a standard way to declare an unused variable.

+4
Feb 15 '14 at 16:10
source share

One option is to take advantage of the fact that compilers usually don’t warn about unused variables when they don’t have nontrivial destructors, and write a generic template wrapper so that there are actual values ​​that you execute and return dummy objects. Something like that:

 template <class RangeType> class UnusedRange { public: UnusedRange(RangeType& wrapped_range) : wrapped_range_(wrapped_range) {} // Explicit destructor makes compiler not complain about unused vars. class UnusedVal { public: ~UnusedVal() {} }; class Iterator { public: typedef decltype(((RangeType*)nullptr)->begin()) WrappedIteratorType; Iterator(WrappedIteratorType wrapped_it) : wrapped_it_(wrapped_it) {} const Iterator& operator++() { ++wrapped_it_; return *this; } bool operator!=(const Iterator& other) { return wrapped_it_ != other.wrapped_it_; } UnusedVal operator*() { return UnusedVal(); } private: WrappedIteratorType wrapped_it_; }; Iterator begin() { return Iterator(wrapped_range_.begin()); } Iterator end() { return Iterator(wrapped_range_.end()); } private: RangeType& wrapped_range_; }; template <class RangeType> UnusedRange<RangeType> Unused(RangeType& range) { return UnusedRange<RangeType>(range); } 

You can use it as follows:

 for (auto unused : Unused(foo)) { ... } 
0
May 19 '15 at 21:19
source share



All Articles