I’m reviewing HTTP Server 3 example on Boost’s website. There is following code in connection class:
boost::tribool result;
boost::tie(result, boost::tuples::ignore) = request_parser_.parse(request_, buffer_.data(), buffer_.data() + bytes_transferred);
where parse declared as
template <typename InputIterator>
boost::tuple<boost::tribool, InputIterator> parse(request& req, InputIterator begin, InputIterator end)
I believe the goal was to copy returned value of tribool to local variable. But what is the point of doing it via temporary object (boost::tie) if one could write something like
boost::tuple<boost::tribool, char*> result = request_parser_.parse(request_, buffer_.data(), buffer_.data() + bytes_transferred);
// Our tribool is available via result.get<0>();
?
The benefit is not performance, it is practicality and readability: since you are not interested by the second object returned by
parse, there is no need for keeping it. It is better to ignore it entirely and only get the result you are effectively interested in, namely thetribool. The following code, which usesresult, will be much clearer.In fact, when a function returns multiple data, it is often useful (in terms of readability) to “split” it to get the individual elements separately. For instance, consider
std::set<T>::insert, which returns an iterator to the element as well as a boolean indicating whether it was newly inserted. Which of the following code do you find clearer:vs
In my opinion, the latter is easier to read.