Skip to content

Instantly share code, notes, and snippets.

@aokomoriuta
Last active August 26, 2015 03:03
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 aokomoriuta/e09bb04bbdca194032ac to your computer and use it in GitHub Desktop.
Save aokomoriuta/e09bb04bbdca194032ac to your computer and use it in GitHub Desktop.
C++14だとけっこうすっきりする
#include <utility>
#include <boost/fusion/include/transform.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/include/invoke.hpp>
class S
{
private:
int a;
long b;
public:
S(const int aa, const long bb)
: a(aa), b(bb) {}
int A() const
{
return a;
}
long B() const
{
return b;
}
template<typename FUNC, typename... ARGS>
auto F(FUNC&& f, ARGS&&... args) const
{
auto a = boost::fusion::transform(boost::fusion::make_vector(std::forward<ARGS>(args)...),
[this](auto f)
{
return (this->*f)();
});
return boost::fusion::invoke(f, a);
}
};
int Foo(const S& s)
{
return s.F([](int a)
{
return a*5;
}, &S::A);
}
long Bar(const S& s)
{
return s.F([](int a, long b)
{
return b + a;
}, &S::A, &S::B);
}
int foo(const S& s)
{
return s.A()*5;
}
long bar(const S& s)
{
return s.B() + s.A();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment