Created
February 15, 2021 06:06
-
-
Save 3noch/407e9ab0753292837a55a9cd1c06a2dc to your computer and use it in GitHub Desktop.
NonEmpty type for C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T, typename Container = std::vector<T>> | |
struct NonEmpty { | |
using iterator = typename Container::iterator; | |
using const_iterator = typename Container::const_iterator; | |
iterator begin() | |
{ | |
return c_.begin(); | |
} | |
iterator end() | |
{ | |
return c_.end(); | |
} | |
const_iterator begin() const | |
{ | |
return c_.begin(); | |
} | |
const_iterator end() const | |
{ | |
return c_.end(); | |
} | |
size_t size() const { return c_.size(); } | |
private: | |
Container c_; | |
NonEmpty(Container const & c) : c_(c) {} | |
NonEmpty(Container &&c) : c_(std::move(c)) {} | |
friend boost::optional<NonEmpty<T, Container>> nonEmpty(Container const &) {} | |
//friend boost::optional<NonEmpty<T, Container>> nonEmpty(Container &&) {} | |
}; | |
template<typename T, typename Container = std::vector<T>> | |
inline boost::optional<NonEmpty<T, Container>> nonEmpty(Container const & vs) { | |
return vs.size() > 0 ? NonEmpty<T, Container>(vs) : boost::optional<NonEmpty<T, Container>>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment