Skip to content

Instantly share code, notes, and snippets.

@qsorix
Created July 24, 2010 09:10
Show Gist options
  • Save qsorix/488558 to your computer and use it in GitHub Desktop.
Save qsorix/488558 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
template <typename T, typename T_Infix>
struct InfixProxy {
T const & lhs_;
T_Infix const & oper_;
InfixProxy(T const & lhs, T_Infix const & oper)
: lhs_(lhs), oper_(oper)
{}
friend T operator>>(const InfixProxy<T, T_Infix>& self, T const & rhs)
{
return self.oper_(self.lhs_, rhs);
}
};
struct Infix {
template <typename T, typename T_Infix>
friend InfixProxy<T, T_Infix> operator<<(T const & lhs, T_Infix const & rhs)
{
return InfixProxy<T, T_Infix>(lhs, rhs);
}
};
struct Add : public Infix
{
template <typename T>
T operator()(T const & lhs, T const & rhs) const
{
return lhs + rhs;
}
} add;
int main()
{
int a = 2, b = 3;
std::cout << (a <<add>> b) << std::endl;
std::string x = "foo", y = "bar";
std::cout << (x <<add>> y) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment