[Boost-bugs] [Boost C++ Libraries] #10393: Add begin/end specialization for optional<T> type

Subject: [Boost-bugs] [Boost C++ Libraries] #10393: Add begin/end specialization for optional<T> type
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-08-22 12:26:04


#10393: Add begin/end specialization for optional<T> type
---------------------------------------+-----------------------
 Reporter: khimru@… | Owner: fcacciola
     Type: Feature Requests | Status: new
Milestone: Boost 1.57.0 | Component: optional
  Version: Boost 1.56.0 | Severity: Cosmetic
 Keywords: optional, for, begin, end |
---------------------------------------+-----------------------
 optional<T> could be used in many places as if it was just T, but, since
 C++ does not apply two levels of constructions one could not use this
 natural looking construct for optional containers:

 if (opt_container) {
   for (auto& element : opt_container) {
      ...
   }
 }

 Simple specialization of begin()/end() functions could solve this problem:

 namespace std {

 template<class T>
 inline auto
 begin(const boost::optional<T>& opt) -> decltype(opt.get().begin()) {
  return opt.get().begin();
 }

 template<class T>
 inline auto
 begin(boost::optional<T>& opt) -> decltype(opt.get().begin()) {
   return opt.get().begin();
 }

 template<class T>
 inline auto
 end(const boost::optional<T>& opt) -> decltype(opt.get().end()) {
   return opt.get().end();
 }

 template<class T>
 inline auto
 end(boost::optional<T>& opt) -> decltype(opt.get().end()) {
   return opt.get().end();
 }

 }

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/10393>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:16 UTC