0

Output parameters in C++ are generally considered a code smell according to the core guidelines. Yet, we have such functions in the regular expressions library

template< class BidirIt,
          class Alloc, class CharT, class Traits >
bool regex_match( BidirIt first, BidirIt last,
                  std::match_results<BidirIt,Alloc>& m,
                  const std::basic_regex<CharT,Traits>& e,
                  std::regex_constants::match_flag_type flags =
                      std::regex_constants::match_default );

in which m is an output parameter. Is there a specific reason for breaking the core guidelines here and not simply returning the std::match_results by value?

  • My quick guess: due to `regex_match()` being an overloaded function, with variants not returning a match result at all. Hence a bool was used as a return value. – klaus triendl Jul 04 '21 at 08:10
  • 3
    The core guidelines don't describe anything as a code smell. In this case, they suggest *preferring* return values over output arguments. That wording does not mean "Thou shalt not ..." - it means "use return values unless you have a particular reason to use output parameters". The reason, in the case you've picked, is that the function returns more than one type of output to the user - and they've chosen to return `bool` (to indicate if a match is found). Among other things that avoids forcing the caller to do a relatively expensive check if the set of matches (output parameter) is empty – Peter Jul 04 '21 at 08:36

1 Answers1

1

There are some exceptions too:

  • For non-value types, such as types in an inheritance hierarchy, return the object by unique_ptr or shared_ptr.
  • If a type is expensive to move (e.g., array), consider allocating it on the free store and return a handle (e.g., unique_ptr), or passing it in a reference to non-const target object to fill (to be used as an out-parameter).
  • To reuse an object that carries capacity (e.g., std::string, std::vector) across multiple calls to the function in an inner loop: treat it as an in/out parameter and pass by reference.

I think it is the 2nd case for regxr match.

Afshin
  • 8,839
  • 1
  • 18
  • 53