您好,登錄后才能下訂單哦!
Clang3.9使用變參模擬實(shí)現(xiàn)CheckerFn和Checker
一,使用變參實(shí)現(xiàn)CheckerFn
1,頭文件
/********************************* * * Author : szyu * * Date : 2017.1.4 * ***********************************/ #ifndef __SZYU_CLANG__ #define __SZYU_CLANG__ #include <iostream> /******************************** * * clang3.9使用變參實(shí)現(xiàn)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,測(cè)試用例
/******************************** * * 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,執(zhí)行結(jié)果
二,使用變參實(shí)現(xiàn)Checker
1,頭文件
/********************************* * * Author : szyu * * Date : 2017.1.4 * ***********************************/ #ifndef __SZYU_CLANG__ #define __SZYU_CLANG__ #include <iostream> /******************************** * * clang3.9使用變參實(shí)現(xiàn)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,測(cè)試用例
/********************************* * * 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,執(zhí)行結(jié)果
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。