开发者

C++, templates with default parameters

开发者 https://www.devze.com 2023-03-06 05:44 出处:网络
A simplified example: There is an abstract template class GCont representing a general container template <typename Item>

A simplified example:

There is an abstract template class GCont representing a general container

template <typename Item>
struct TList
{
    typedef std::vector <Item> Type;
};


template <typename Item>
class GCont
{
    protected:
            typename TList &l开发者_JAVA百科t;Item>::Type items;

    public:
            typedef Item type;
            virtual ~GCont() = 0 {};
};

and derived abstract template class having one implicit parameter

template < typename Item, const bool par = true>
class GCont2 : public GCont <Item>
{
    public:
            GCont2 () : GCont <Item> () {}
    virtual ~GCont2() = 0 {};
};

and its specialization for pointers

template <typename Item, const bool par>
class GCont2 <Item *, par> : public GCont <Item *>
{
    public:
            GCont2 () : GCont <Item *> () {}
            virtual ~Cont() {}
};

The derived template class Cont

template <typename Point, const bool par = true>
class Cont : public GCont2 <Point, par>
{
    public:
            Cont() : GCont2 <Point, par>() {}
            virtual ~Cont() {}
};

and specialization for pointers

template <typename Point, const bool par>
class Cont <Point *, par> : public GCont2 <Point *, par>
{
    public:
            Cont() : GCont2 <Point *, par> () {}
};

Class Point:

template <typename T>
class Point
{
    protected:
            T x, y, z;
};

Is it possible to write a function test() having an universal formal parameter alowing to use both

Cont <Point <T> *> *points

and

Cont <Point <T> *, false> *points

In my program

template <typename T>
void test (Cont <Point <T> *> *points)
{
std::cout << "test";
}

template <typename T>
void test2 (Cont <Point <T> *, false> *points)
{
std::cout << "test2";
}

int _tmain(int argc, _TCHAR* argv[])
{
Point <double> * p = new Point <double>();
Cont <Point <double> *, false> points;

    test(&points); //Error
     test2(&points); //OK

return 0;
}

During the translation the following error appears:

Error   1   error C2784: 'void test(Cont<Point<T>*> *)' : could not deduce template argument for 'Cont<Point<T>*> *' from 'Cont<Point,par> *'   g:\templates_example.cpp    27

Thanks for your help...

Updated status:

I trimmed the code... But the problem remains... My code does not compile with the same error using MSVS 2010.

I found the partial solution-templatization of the container.

template <typename Container>
void test (Container *points)
{
std::cout << "test";
}


Your code compiles totally fine for me, you're not giving us the code you really have. Aside from that, your problem seems to be that you want to provide the wrong input. Your test function expects a container of pointers to points (Cont< Point<T>* >), but you provide a container of points (Cont< Point<T> >):

Cont< Point<int> >* pc;
// ...
test(pc);

This will of course produce the error you get. You need a

Cont< Point<int>* >* pc;

That is, a container of pointers. You do this correctly in your question, but your real code doesn't seem to be that.

0

精彩评论

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