开发者

C++, template specialization problem

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 C开发者_C百科ontainer
{
    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;
}


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:

 template<typename T>
 class Container<Point<T> > { };


yes you can specialize your class with that type Point <T> .

Edit:

I tried the following code, is it valid?

If you've tried the following code ,don't you know whether it compiled or not ? 0_o

int _tmain(int argc, _TCHAR* argv[])
{
Container <Point <double> > points;
return 0; // return should be here nor program will exit before creating Container <Point <double> > points;
}

r

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜