VC6模板特化的问题

DDGG 2008-11-04 04:45:45
//通用Type1, Type2
template<class T1> struct type_traits
{
template<class T1, class T2> struct Foo
{
Foo(T1 t1, T2 t2)
{
std::cout << "noint+type1" << std::endl;
}
};
};

//特化的int,Type2
// template<> struct type_traits<int>
// {
// template<class Type1, class Type2> struct Foo
// {
// Foo(Type1 t1, Type2 t2)
// {
// std::cout << "int+type2" << std::endl;
// }
// };
// };
//
//Wrapper 类
template<class T1, class T2>
struct FooWapper : public type_traits<T1>::Foo<T1, T2>
{
FooWapper(T1 t1, T2 t2) : type_traits<T1>::Foo<T1, T2>(t1, t2) {}
};


//maker函数用于推演
template<class T1, class T2>
FooWapper<T1, T2> inline make_foo(T1 t1, T2 t2)
{
return FooWapper<T1, T2>(t1, t2);
}

//测试程序
void main()
{
// make_foo(1, 2.0); //调用特化的<int, type2>
// make_foo("a", 2.0); //调用通用的<type1, type2>
}


上面这段网上找的代码用vc6编译没有问题,但是我想特化的是第2个参数,于是把 FooWrapper 类改为:

//Wrapper 类
template<class T1, class T2>
struct FooWapper : public type_traits<T2>::Foo<T1, T2>
{
FooWapper(T1 t1, T2 t2) : type_traits<T2>::Foo<T1, T2>(t1, t2) {}
};


编译就通不过了,报错:
error C2516: 'Foo<`template-parameter257',`template-parameter258'>' : is not a legal base class

请问应该怎么解决?谢谢!
...全文
326 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
DDGG 2008-11-18
  • 打赏
  • 举报
回复
没办法,向VC6认输。。
overbai 2008-11-10
  • 打赏
  • 举报
回复
学习!
太乙 2008-11-10
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
//通用Type1, Type2
template<class T1> struct type_traits
{
template<class T3, class T2> struct Foo
{
Foo(T1 t3, T2 t2)
{
std::cout << "noint+type1" << std::endl;
}
};
};

//特化的int,Type2
// template<> struct type_traits<int>
// {
// template<class Type1, class Type2> struct Foo
// {
// Foo(Type1 t1, Type2 t2)
// {
// std::cout << "int+type2" << std::endl;
// }
// };
// };
//
//Wrapper 类
template<class T1, class T2>
struct FooWapper : public type_traits<T2>::Foo<T1, T2>
{
FooWapper(T1 t1, T2 t2) : type_traits<T2>::Foo<T1, T2>(t1, t2) {}
};



//maker函数用于推演
template<class T1, class T2>
FooWapper<T1, T2> inline make_foo(T1 t1, T2 t2)
{
return FooWapper<T1, T2>(t1, t2);
}

//测试程序
int main()
{
// make_foo(1, 2.0); //调用特化的<int, type2>
// make_foo("a", 2.0); //调用通用的<type1, type2>
return EXIT_SUCCESS;
}





上述代码在mingw下通过!!


//通用Type1, Type2
template<class T1> struct type_traits
{
template<class T3, class T2> struct Foo
{
Foo(T1 t3, T2 t2)
{
std::cout << "noint+type1" << std::endl;
}
};
};






hzc191025 2008-11-10
  • 打赏
  • 举报
回复
在vc6.0下,
template<class T1, class T2>
struct FooWapper : public type_traits<T2>::Foo<T1, T2>
{
FooWapper(T1 t1, T2 t2) : type_traits<T2>::Foo<T1, T2>(t1, t2) {}
};

这两个要一样
jackzhhuang 2008-11-10
  • 打赏
  • 举报
回复
VC6.0对模板的“支持”让我对微软失望好久,直到发现了vs2005……
Jarrys 2008-11-09
  • 打赏
  • 举报
回复
在下也只能顶一下罗。
xxgamexx 2008-11-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 DDGG 的回复:]
//通用Type1, Type2
template <class T1> struct type_traits
{
template <class T1, class T2> struct Foo
{
Foo(T1 t1, T2 t2)
{
std::cout < < "noint+type1" < < std::endl;
}
};
};

这里两个T1是没有关系的吧,我把第2个改成T3也不影响的,原来的代码还是可以编译通过,现在的还是一样的错!

//通用Type1, Type2
template <class T1> struc…
[/Quote]

没有关系的~
DDGG 2008-11-06
  • 打赏
  • 举报
回复
-__-!
deerwin1986 2008-11-05
  • 打赏
  • 举报
回复
有道理 VC6搞模版是有名的痛苦 支持得那叫一个"彻底"啊...
e_sharp 2008-11-05
  • 打赏
  • 举报
回复
用模板,还是放弃vc6吧
DDGG 2008-11-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 taodm 的回复:]
玩模板就不要用VC6来浪费生命。先换了VC2005、gcc编译了再说。
[/Quote]

不要啊,老大!我现在不可能换到2005……
taodm 2008-11-04
  • 打赏
  • 举报
回复
玩模板就不要用VC6来浪费生命。先换了VC2005、gcc编译了再说。
DDGG 2008-11-04
  • 打赏
  • 举报
回复
//通用Type1, Type2
template<class T1> struct type_traits
{
template<class T1, class T2> struct Foo
{
Foo(T1 t1, T2 t2)
{
std::cout << "noint+type1" << std::endl;
}
};
};

这里两个T1是没有关系的吧,我把第2个改成T3也不影响的,原来的代码还是可以编译通过,现在的还是一样的错!

//通用Type1, Type2
template<class T1> struct type_traits
{
template<class T3, class T2> struct Foo
{
Foo(T3 t1, T2 t2)
{
std::cout << "noint+type1" << std::endl;
}
};
};


yshuise 2008-11-04
  • 打赏
  • 举报
回复
要换一个编译器再来讨论吧
太乙 2008-11-04
  • 打赏
  • 举报
回复
我没看出来特化啊?

xuedaoli 2008-11-04
  • 打赏
  • 举报
回复
你这里的原来代码是这样的
template<class T1> struct type_traits
{
template<class T1, class T2> struct Foo
{}
}

template<class T1, class T2>
struct FooWapper : public type_traits<T2>::Foo<T1, T2>
{
}
而上面的type_traits<T2>传入的是T2,而编译器就推导出template<class T1, class T2> struct Foo这里的T1 == T2
接着你又传递进去T1,造成无法推导出类型

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧