0

I have a function that returns a std::vector of std::strings. However if some check is not met at the beginning of the function, I bail out (for example there is no such user)

What would the correct semantics be for such a case, where I have nothing to return?

3
  • Is this an error condition or is it to be expected during normal operation of your program? Commented Aug 6, 2013 at 18:10
  • 1
    Maybe pass a vector<string> byref, fill it, and return a bool. Commented Aug 6, 2013 at 18:11
  • (In C, you would return a char ** and it would be NULL on error.) Commented Aug 6, 2013 at 18:12

2 Answers 2

1

If this in an error condition, then you should throw an exception. If an empty list is considered normal operation, then return an empty vector. The answer will depend heavily on your application.

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

Comments

0

In your case it's easy because you have std::vector which can be empty. If you want to mark a result to be invalid you can use boost::optional.

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.