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?

2
  • 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. Commented Jul 4, 2021 at 8: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 Commented Jul 4, 2021 at 8:36

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.