Reputation: 493
I've got a linked-list class and I've just written a set of functions to make it work with range-based for. To use the same class template for the forward iterator (such as you get with begin() ) as the reverse iterator (such as you get with rbegin() ), I added a bool to the template arguments. during increment or decrement, the bool is checked one time. Will it be optimized away by the compiler or will the check happen at runtime?
template< typename TYPE >
template< typename LINKTYPE, typename LINK, const bool REVERSE >
LinkList<TYPE>::IteratorBase<LINKTYPE, LINK, REVERSE> LinkList<TYPE>::IteratorBase<LINKTYPE, LINK, REVERSE>::operator++( int ) {
Link<TYPE> *old = cur;
if ( cur ) {
cur = REVERSE ? cur->GetPrevious() : cur->GetNext();
return old;
}
cur = nullptr;
return old;
}
here is a more complete excerpt of the code: http://pastebin.com/vv7Wgm9T
Upvotes: 3
Views: 536
Reputation: 96281
There is nothing in the C++ standard about when to perform an optimization like this, so it's strictly compiler-specific. Thus the only way to find out for sure is to either consult your compiler's documentation or to compile the code and look at the disassembly. Most likely it will be optimized away but there are no guarantees as to that behavior.
Upvotes: 4