64,649
社区成员
发帖
与我相关
我的任务
分享
//总结一点点模板中比较怪的语法
template<typename X>
struct A;
template<typename T, typename T1>
void fuck1(A<T1>);
template<typename T, typename T1>
void fuck2(A<T1>);
template<typename X>
struct A
{
typedef int* intptr;
//类模板的函数模板
template<typename Y> void fuck();
//绑定的模板友元,注意这里的第一个参数绑定到了int
friend void fuck1 <int, X> (A);
//非绑定的模板友元
template<typename T, typename T1> friend void fuck2(A);
private:
int v;
};
//模板的模板参数
template<template <typename> class T>//class不能换
class B{};
//示例typename和template的用法
template<typename T>
void foo(T t)
{
typename T::intptr x;//typename不能省
t.template fuck<int>();//template不能省
}
//类模板的函数模板定义语法
template<typename X>
template<typename Y>
void A<X>::fuck(){}
//定义这两个函数模板
template<typename T, typename T1>
void fuck1(A<T1> a){a.v = 1;}
template<typename T, typename T1>
void fuck2(A<T1> a){a.v = 1;}
//显示实例化
template class A<int>;
//template void fuck1<float>(A<float>);出错
template void fuck1<int>(A<int>);
template void fuck2<A<int>, A<double> >(A<A<double> >);
int main()
{
A<int> a;
B<A> b;
fuck1<int>(a);
//fuck1<float>(a);出错
fuck2<int>(a);
fuck2<double>(a);
return 0;
}
template<typename X>
class A
{
public:
typedef int* intptr;
template<typename Y> void fuck(){}
};
template<template <typename> class T>//class不能换
class B{};
template<typename T>
void foo(T t)
{
typename T::intptr x;//typename不能省
t.template fuck<int>();//template不能省
}
int main()
{
B<A> a;
return 0;
}