[C 模板]Clang3.9使用变参模拟实现CheckerFn和Checker

2017-01-05 08:53:58来源:作者:人点击

第七城市
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"voidfunc11(){ std::cout << "In func11" << std::endl;}voidfunc12( int v ){ std::cout << "In func12" << std::endl; std::cout << "Args..." << v << std::endl;}voidfunc13( int v, char ch ){ std::cout << "In func13" << std::endl; std::cout << "Args1..." << v << ", Args2..." << ch << std::endl;}voidtest1(){ 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 );}intmain( 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"voidtest1(){ Checker<Register1>::_register(); std::cout << "**********************************" << std::endl; Checker<Register1, Register2>::_register(); std::cout << "**********************************" << std::endl; Checker<Register1, Register2, Register3>::_register();}intmain( int argc, char *argv[] ){ test1(); return 0;}
3,执行结果


第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台