www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



c++ - pointer with typename

↑ ↓ ← sevki <mesti_mudam yahoo.com> writes:
Hi. This code doesnt compile with gcc, dmc, icc.
But I think it should :)
any comments...
Mar 22 2007
↑ ↓ 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
↑ ↓ 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
→ 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
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
↑ ↓ → 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
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
↑ ↓ → 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