Clang3.9使用变参模拟实现CheckerFn和Checker
一,使用变参实现CheckerFn
1,头文件
/********************************* * * Author : szyu * * Date : 2017.1.4 * ***********************************/ #ifndef __SZYU_CLANG__ #define __SZYU_CLANG__ #include <iostream> /******************************** * * clang3.9使用变参实现CheckerFn * *********************************/ template <typename T> class CheckerFn; template <typename T, typename... Args> class CheckerFn<T(Args...)> { private: typedef T (*Func)(Args...); Func Fn; public: CheckerFn() { std::cout << "In CheckerFn::CheckerFn()" << std::endl; } CheckerFn( Func f ) : Fn( f ) { std::cout << "In CheckerFn::CheckerFn(Func f)" << std::endl; } T operator()( Args... args) const { Fn( args... ); } }; #endif
2,测试用例
/******************************** * * Author : szyu * * Date : 2017.1.4 * ***********************************/ #include "param.h" void func11() { std::cout << "In func11" << std::endl; } void func12( int v ) { std::cout << "In func12" << std::endl; std::cout << "Args..." << v << std::endl; } void func13( int v, char ch ) { std::cout << "In func13" << std::endl; std::cout << "Args1..." << v << ", Args2..." << ch << std::endl; } void test1() { typedef CheckerFn<void ()> CheckOne; CheckOne one( func11 ); one(); std::cout << "**********************************" << std::endl; typedef CheckerFn<void (int)> CheckTwo; CheckTwo two( func12 ); int value = 7; two( value ); std::cout << "**********************************" << std::endl; typedef CheckerFn<void (int, char)> CheckThree; CheckThree three( func13 ); value = 9; char ch = 'B'; three( value, ch ); } int main( int argc, char *argv[] ) { test1(); return 0; }
3,执行结果
二,使用变参实现Checker
1,头文件
/********************************* * * Author : szyu * * Date : 2017.1.4 * ***********************************/ #ifndef __SZYU_CLANG__ #define __SZYU_CLANG__ #include <iostream> /******************************** * * clang3.9使用变參实现Checker * *********************************/ class Register1 { public: static void _register() { std::cout << "In Register1::_register()" << std::endl; } }; class Register2 { public: static void _register() { std::cout << "In Register2::_register()" << std::endl; } }; class Register3 { public: static void _register() { std::cout << "In Register3::_register()" << std::endl; } }; template <typename T1, typename... Args> class Checker : public T1, public Args... { public: static void _register() { std::cout << "In Checker<T1, Args...>::_register()" << std::endl; T1::_register(); Checker<Args...>::_register(); } }; template <typename T1> class Checker<T1> { public: static void _register() { std::cout << "In Checker<T1>::_register()" << std::endl; T1::_register(); } }; #endif
2,测试用例
/********************************* * * Author : szyu * * Date : 2017.1.4 * ***********************************/ #include "param2.h" void test1() { Checker<Register1>::_register(); std::cout << "**********************************" << std::endl; Checker<Register1, Register2>::_register(); std::cout << "**********************************" << std::endl; Checker<Register1, Register2, Register3>::_register(); } int main( int argc, char *argv[] ) { test1(); return 0; }
3,执行结果
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。