1 //define two types first2 structa{};3 structb{};4 5 classFoo6 {7 Public:8 //bind type A to Foo9 typedef A ClassType;Ten One voidfoo () A { -Std::cout << __function__ <<Std::endl; - } the }; - - classBar - { + Public: - //bind type B to bar + typedef B ClassType; A at voidBar () - { -Std::cout << __function__ <<Std::endl; - } - }; - in //Optional, define a traits class for Foo bar -Template<typename t> to classtest_traits + { - Public: the typedef typename T::classtype ClassType; * }; $ Panax Notoginseng //define a template function with Foo Bar as argument -Template<typename t> the voidfunc () + { A //If you do not define the traits class, you can use T::classtype directly here theDo_func (T (), test_traits<t>:: ClassType ()); + } - $ //the implementation of the Func function really type a class $Template<typename t> - voidDo_func (T &ins, a a) - { the Ins.foo (); - }Wuyi the //implementation of the Func function really B type class -Template<typename t> Wu voidDo_func (T &ins, B a) - { About Ins.bar (); $ } - - intMainvoid) - { AFunc<foo>(); +Func<bar>(); the}
Defining the Trais Class