65,210
社区成员
发帖
与我相关
我的任务
分享template <typename T1,typename T2>
class CMyTemplate
{
public:
template<typename _Ty>
void func(const T1 & t1,const T2, _Ty t)//错误: 模板参数表太多
{
....;
}
};template <> void p_func<std::string>(const std::string & ty);void p_func(const std::string & ty);[Example:
template<class T1> class A {
template<class T2> class B {
template<class T3> void mf1(T3);
void mf2();
};
};
template<> template<class X>
class A<int>::B { };
template<> template<> template<class T>
void A<int>::B<double>::mf1(T t) { }
template<class Y> template<>
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
// its enclosing class template A is not
!end example] [Example:
template<class T> struct A {
void f(T);
template<class X1> void g1(T, X1);
template<class X2> void g2(T, X2);
void h(T) { }
};
// specialization
template<> void A<int>::f(int);
// out of class member template definition
template<class T> template<class X1> void A<T>::g1(T, X1) { }
// member template specialization
template<> template<class X1> void A<int>::g1(int, X1);
// member template specialization
template<> template<>
void A<int>::g1(int, char); // X1 deduced as char
template<> template<>
void A<int>::g2<char>(int, char); // X2 specified as char
// member specialization even if defined in class definition
template<> void A<int>::h(int) { }
!end example]01 : #include <string>
02 : #include <iostream>
03 :
04 : template <typename T1,typename T2>
05 : class CMyTemplate
06 : {
07 : template <typename _Ty>
08 : void p_func(const _Ty & ty)
09 : {
10 : std::cout<<"const _Ty & ty"<<std::endl;
11 : }
12 : template <>
13 : void p_func<std::string>(const std::string & ty)
14 : {
15 : std::cout<<"const std::string & ty"<<std::endl;
16 : }
17 : public:
18 : void my_func(const T1 & t1,const T2 & t2)
19 : {
20 : p_func(t2);
21 : std::cout<<"const T1 & t1,const T2 & t2"<<std::endl;
22 : }
23 : template <typename _Ty>
24 : void my_func(const T1 & t1,const _Ty & ty)
25 : {
26 : p_func(ty);
27 : std::cout<<"const T1 & t1,const _Ty & ty"<<std::endl;
28 : }
29 : };
30 :
31 : int main(int argc, char * argv[])
32 : {
33 : std::string str="abc";
34 : CMyTemplate<int,int> my_int;
35 : my_int.my_func(1,2);
36 : my_int.my_func(1,str);
37 : return 0;
38 : }
39 :
#include <string>
#include <iostream>
template <typename T1,typename T2>
class CMyTemplate
{
template <typename _Ty>
void p_func(const _Ty & ty)
{
std::cout<<"const _Ty & ty"<<std::endl;
}
template <>
void p_func<std::string>(const std::string & ty)
{
std::cout<<"const std::string & ty"<<std::endl;
}
public:
void my_func(const T1 & t1,const T2 & t2)
{
p_func(t2);
std::cout<<"const T1 & t1,const T2 & t2"<<std::endl;
}
template <typename _Ty>
void my_func(const T1 & t1,const _Ty & ty)
{
p_func(ty);
std::cout<<"const T1 & t1,const _Ty & ty"<<std::endl;
}
};
int main(int argc, char * argv[])
{
std::string str="abc";
CMyTemplate<int,int> my_int;
my_int.my_func(1,2);
my_int.my_func(1,str);
return 0;
}
//估计是别的地方错了,偶试了这段代码没问题
#include <cstdlib>
#include <iostream>
using namespace std;
template <typename T1,typename T2>
class CMyTemplate
{
public:
template<typename _Ty>
void func(const T1 & t1,const T2, _Ty t)
{
//....;
}
};
int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}