Next: High Integrity CPP Rule 17.15  Up: 17 Standard Template Library (STL)  Previous: High Integrity CPP Rule 17.13  Contents

High Integrity CPP Rule 17.14   Return false for equivalent values in relational predicates.

Justification

Sorted containers, and algorithms that operate on sorted containers, require comparison predicates that define the sort order of the elements. These predicates are used to test if elements are equal, they do so by checking that neither element preceeds the other in the sort order.

Returning true from a comparison predicate for equivalent elements means the container will never detect that elements are equal, resulting in an invalid state.

   
   // Potential algorithm determining equivalent elements for sorted
   // containers.
   //
   bool areElementsEqual( T& a, T& b )
   {
      // pred is a comparison predicate that defines the sort order
      // of a & b.
      //
      if ( !pred( a, b ) && !pred( b, a ) )
      {
         // a and b are equivalent
      }
   }
See also

Rule 17.15

Reference

Effective STL Item 21;


HICPP VERSION 2.4  http://www.codingstandard.com   Copyright: © 2007 THE PROGRAMMING RESEARCH GROUP