您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。