Last active
August 26, 2015 03:03
-
-
Save aokomoriuta/e09bb04bbdca194032ac to your computer and use it in GitHub Desktop.
C++14だとけっこうすっきりする
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
#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