Thread overview
pointer with typename
Mar 23, 2007
sevki
Mar 23, 2007
Bertel Brander
Mar 23, 2007
sevki
Mar 23, 2007
Bertel Brander
Mar 23, 2007
Arjan Knepper
Mar 23, 2007
Bertel Brander
Mar 24, 2007
Arjan Knepper
Mar 24, 2007
sevki
March 23, 2007
Hi. This code doesnt compile with gcc, dmc, icc.
But I think it should :)
any comments...
March 23, 2007
sevki skrev:
> Hi. This code doesnt compile with gcc, dmc, icc.
> But I think it should :)
> any comments...

I don't think that a class can inherit from a template class
that depend on the class itself.

The basic code that fails is:
template <class T>
class Base
{
protected:
	typename T::privIC * priv;
public:
	virtual int f1() = 0;
	virtual int f2() = 0;
};

class A : public Base<A>
{
public:
};

If you tell us what you are trying to do, we might
be able to find a way out.

-- 
Just another homepage:
http://damb.dk
But it's mine - Bertel
March 23, 2007
well, it can, and a good example of it is "curiously recurring template" pattern. go get a c++ book..
March 23, 2007
sevki skrev:
> well, it can, and a good example of it is "curiously recurring template" pattern.
> go get a c++ book..

I don't read books, I solve problems.

Is there any practical application for "recurring template",
that can't be better solved otherwice?

-- 
Just another homepage:
http://damb.dk
But it's mine - Bertel
March 23, 2007
sevki wrote:
> well, it can, and a good example of it is "curiously recurring template" pattern.
> go get a c++ book..

Can not decode the gene.cpp  file attachment, but afaik gcc and dmc are perfectly well able to compile the "curiously recurring template" pattern.

Arjan
March 23, 2007
Arjan Knepper skrev:
> sevki wrote:
>> well, it can, and a good example of it is "curiously recurring template" pattern.
>> go get a c++ book..
> 
> Can not decode the gene.cpp  file attachment

This was the attachment:

#ifdef __DMC__
#include <iostream.h>
#else
#include <iostream>
using std::cout;
using std::endl;
#endif

template <class T>
class Base
{
protected:
	typename T::privIC * priv;
public:
	virtual int f1() =0;
	virtual int f2() =0;
};

class A : public Base<A>
{
public:
	class privIC
	{
		int i;
	};
	int f1();
	int f2();
};

int A::f1()
{
	return 0;
}

int A::f2()
{
	return 0;
}

int main()
{
	A a;
	cout << a.f2() << endl;
}

-- 
Just another homepage:
http://damb.dk
But it's mine - Bertel
March 24, 2007
sevki wrote:
> well, it can, and a good example of it is "curiously recurring template" pattern.
> go get a c++ book..

Bertel thanks for decoding the atachment.

The problem is NOT the "curiously recurring template" pattern. But the fact the type used as argument (A) is incomplete at instatiating the template Base class with arg class A but B needs the complete arg type A to determine the A::privIC type.

I expect most if not all compilers will fail on this particular construct.

this for example does work but is NOT a "curiously recurring template":

class A; // incomplete type


template <class T>
class Base
{
protected:
    typedef typename T::privIC Tpriv;
    Tpriv  *tprivptr;
public:
    virtual int f1() =0;
    virtual int f2() =0;
};

class A // complete type
{
public:
    class privIC
    {
        int i;
    };
};

class C : public Base < A > //no problem since the type A is complete
{
public:
    int f1();
    int f2();
};


/////////////////////////////////////////////////////////
// This also works and IS a "curiously recurring template":

class A;
class privIC
{
    friend class A;
    int i;
};

template <class T>
class Base
{
protected:
    T    *tptr;

public:
    virtual int f1() =0;
    virtual int f2() =0;

    void FiddleWithprivIC () { tptr -> fx (); }
};

class A : public Base < A >
{
private:
    privIC  *ppic;

public:
    int f1();
    int f2();
    void fx() { ppic -> i = 0; }
};

HTH
Arjan Knepper
March 24, 2007
I want to keep privC "in the scope" of a class:
There will be
class A : public Base<A>
class B : public Base<B>
etc.
all having the "same" interface, and all of them's private implementation is
called privC. so either in A, B, etc. or later I'll define class A::privC, class
B::privC, etc..

In the code that I've sent, if one replaces "typename T::privC * priv" with "T *
priv" then it's valid and it compiles, BUT STILL "the type used as argument (A) is
incomplete at instatiating the
template Base class"...

also, "the argument type being incomplete" is the core feature of "curiously recurring template pattern", and i know there is no problem with it. I think the problem is with the C++ standard. The code I've sent is, to my opinion, clear and should be compilable. or least it is as compilable as the usual use of the curiously recurring template pattern.

thx,
Sevki