Created
January 7, 2015 05:02
-
-
Save m00shm00sh/c585c90de90ba11b0435 to your computer and use it in GitHub Desktop.
Stream operators for std::stack
This file contains hidden or 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
#include <iostream> | |
#include <stack> | |
#include <type_traits> | |
#include <boost/tti/has_member_function.hpp> | |
BOOST_TTI_HAS_MEMBER_FUNCTION(push) | |
BOOST_TTI_HAS_MEMBER_FUNCTION(pop) | |
BOOST_TTI_HAS_MEMBER_FUNCTION(top) | |
template <typename C> | |
constexpr bool has_push() { | |
return has_member_function_push<C, void, boost::mpl::vector<typename C::value_type const&>>::value; | |
} | |
template <typename C> | |
constexpr bool has_pop() { | |
return has_member_function_pop<C, void>::value; | |
} | |
template <typename C> | |
constexpr bool has_top() { | |
return has_member_function_top<C, typename C::reference>::value; | |
} | |
template <typename C, typename> | |
constexpr bool is_possible() { | |
return has_push<C>() && has_pop<C>() && has_top<C>(); | |
} | |
template <typename C, typename U = typename C::value_type> | |
auto possible() -> typename std::enable_if<is_possible<C,U>(), C>::type; | |
template <typename C> | |
auto operator<< (C&& c, typename C::value_type const& v) -> decltype(possible<C>())&& { | |
c.push(v); | |
return std::move(c); | |
} | |
template <typename C> | |
auto operator<< (C& c, typename C::value_type const& v) -> decltype(possible<C>())& { | |
c.push(v); | |
return c; | |
} | |
template <typename C> | |
auto operator>> (C&& c, typename C::value_type& v) -> decltype(possible<C>())&& { | |
v = c.top(); | |
c.pop(); | |
return std::move(c); | |
} | |
template <typename C> | |
auto operator>> (C& c, typename C::value_type& v) -> decltype(possible<C>())& { | |
v = c.top(); | |
c.pop(); | |
return c; | |
} | |
template <typename T> auto COPY(T t) -> T { return t; } | |
int main(){ | |
std::stack<int> st; | |
st << 1 << 2; | |
int a,b; | |
auto _t0 = COPY(st) << 3; | |
auto _t1 = COPY(st >> a) >> b; | |
// force a deref of null so we get into gdb, since gdb's pretty-printer works on std::stack | |
*(char volatile*)nullptr = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment