C++ 模板元编程
#include <iostream>
#include <type_traits>
/// pointer to class member function as template paramter
template<class _C, typename _MFN, typename ..._Args>
void mem_function_invoker(const _C &c, _MFN _C::* p, _Args &&...args) {
(c.*p)(std::forward<_Args>(args)...);
}
struct Test {
void test() const {
std::cout << __FUNCTION__ << "() was called.\n";
}
void test(const std::string &str) const {
std::cout << __FUNCTION__ << "(const std::string &) was called with argument as " << str << ".\n";
}
};
int main(int argc, char *argv[]) {
Test t;
void (Test::*test_v1)() const = &Test::test;
void (Test::*test_v2)(const std::string &) const = &Test::test;
mem_function_invoker(t, test_v1);
mem_function_invoker(t, test_v2, "Hello,C++ metaprogramming!");
}
|