Skip to content

Instantly share code, notes, and snippets.

@3noch
Created February 15, 2021 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3noch/407e9ab0753292837a55a9cd1c06a2dc to your computer and use it in GitHub Desktop.
Save 3noch/407e9ab0753292837a55a9cd1c06a2dc to your computer and use it in GitHub Desktop.
NonEmpty type for C++
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