www.digitalmars.com         C & C++   DMDScript  

c++ - pointer with typename

reply sevki <mesti_mudam yahoo.com> writes:
Hi. This code doesnt compile with gcc, dmc, icc.
But I think it should :)
any comments...
begin 644 dene.cpp
M(VEF9&5F(%]?1$U#7U\*(VEN8VQU9&4 /&EO<W1R96%M+F ^"B-E;'-E"B-I
M;F-L=61E(#QI;W-T<F5A;3X*=7-I;F< <W1D.CIC;W5T.PIU<VEN9R!S=&0Z
M.F5N9&P["B-E;F1I9 H*=&5M<&QA=&4 /&-L87-S(%0^"F-L87-S($)A<V4*
M>PIP<F]T96-T960Z" ET>7!E;F%M92!4.CIP<FEV24, *B!P<FEV.PIP=6)L
M:6,Z" EV:7)T=6%L(&EN="!F,2 I(#TP.PH)=FER='5A;"!I;G0 9C(H*2`]
M,#L*?3L*"F-L87-S($$ .B!P=6)L:6, 0F%S93Q!/ I["G!U8FQI8SH*"6-L
M87-S('!R:79)0PH)>PH)"6EN="!I.PH)?3L*"6EN="!F,2 I.PH):6YT(&8R
M*"D["GT[" II;G0 03HZ9C$H*0I[" ER971U<FX ,#L*?0H*:6YT($$Z.F8R
M*"D*>PH)<F5T=7)N(#`["GT*"FEN="!M86EN*"D*>PH)02!A.PH)8V]U="`\
4/"!A+F8R*"D /#P 96YD;#L*?0H`
`
end
Mar 22 2007
parent reply Bertel Brander <bertel post4.tele.dk> writes:
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
Mar 23 2007
parent reply sevki <mesti_mudam yahoo.com> writes:
well, it can, and a good example of it is "curiously recurring template"
pattern.
go get a c++ book..
Mar 23 2007
next sibling parent Bertel Brander <bertel post4.tele.dk> writes:
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
Mar 23 2007
prev sibling next sibling parent reply Arjan Knepper <arjan ask.me.to> writes:
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
Mar 23 2007
parent Bertel Brander <bertel post4.tele.dk> writes:
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
Mar 23 2007
prev sibling parent reply Arjan Knepper <arjan ask.me.to> writes:
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
Mar 24 2007
parent sevki <mesti_mudam yahoo.com> writes:
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
Mar 24 2007