C++,模板特化问题
我在模板类专业化方面遇到问题,请参阅下面的代码。
template <typename T>
class Point
{
private
T x, y;
typedef T Type;
public:
Point ( const T & x_, const T & y_) : x ( x_ ), y ( y_ ) {}
};
template <typename Item>
struct TItems
{
typedef std::vector <Item> Type;
};
template <typename Item>
class Container
{
protected:
typename TItems <Item>::Type items;
public:
typedef Item type;
};
是否可以为 Point 专门化 Container 类?
更新的问题:
我尝试了以下代码,它有效吗?
template <typename T>
class Container < Point <T> >
{
};
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
Container <Point <double> > points;
}
I am having problems with template class specialization, see the code bellow, please.
template <typename T>
class Point
{
private
T x, y;
typedef T Type;
public:
Point ( const T & x_, const T & y_) : x ( x_ ), y ( y_ ) {}
};
template <typename Item>
struct TItems
{
typedef std::vector <Item> Type;
};
template <typename Item>
class Container
{
protected:
typename TItems <Item>::Type items;
public:
typedef Item type;
};
Is it possible to specialize Container class for Point ?
Updated question:
I tried the following code, is it valid?
template <typename T>
class Container < Point <T> >
{
};
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
Container <Point <double> > points;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以,但是你的语法不太正确。就目前情况而言,编译器不知道
T
是什么,因此您必须告诉它它是一个模板参数:You can, yes, but your syntax isn't quite right. As it stands, the compiler doesn't know what
T
is, so you have to tell it that it is a template parameter:是的,您可以使用该类型专门化您的类
Point
。编辑:
如果您尝试过以下代码,您不知道它是否编译通过? 0_
或
yes you can specialize your class with that type
Point <T>
.Edit:
If you've tried the following code ,don't you know whether it compiled or not ? 0_o
r