Topic: Inheritance and template with g++ and eg++


Author: AllanW@my-dejanews.com
Date: 1999/01/19
Raw View
In article <77vqab$5c7$1@news-sop.inria.fr>,
  hprieto@sophia.inria.fr (Helene Prieto) wrote:
>
> I have some problems with inheritance and template...
> Here is my code :

I've taken a few liberties:

  1) I resolved all macros, and removed the definitions.
  2) I renamed template class S to class BaseT
  3) I removed struct VOID since it wasn't used.
  4) I changed main() to have a return type of int, as is required.

    #include <iostream.h>

    template<class T>
    struct BaseT // Formerly S<T>
    { };

    template<class T>
    void Prt(BaseT<T> & p)
    {
        (*static_cast<T*>(&p)).print();
    }

    struct A: public BaseT<A>
    {
        int n;
        A(): n(0) {}
        A(int i): n(i) {}
        void hello() {cout <<"hello ";}
        void print() {cout <<n<<" AA"<<endl;}
    };

    struct B: public BaseT<B>, public A
    {
        B() : A(0) {}
        B(int n) : A(n) {}
        void hello() {cout <<"Hello ";}
        void print() {hello();cout <<"BB"<<endl;}
    };

    int main()
    {
        A a(1);
        B b(4);

        Prt(a);
        Prt(b);
        return 0;
    }

> And this is the result of compilation with g++ or eg++ :
>
>  /*
>  Essai.cxx: In function `int main()':
>  Essai.cxx:36: no matching function for call to `Prt (B &)'
>  *** Error code 1
>  make: Warning: Target `Essai.exe' not remade because of errors
>  */

FWIW, Microsoft VC5 compiles with no errors.

> So the instance of B is clearly not recognize as a S<B> despite of the
> derivation...

Or, with my changes, B is derived from BaseT<B>, and yet doesn't match
the template.

> So I think it is a problem with g++ and eg++. Does someone can help me for
> solution similarly (without virtual methods), or a way to force eg++ to
> understand the hierarchy ?

I'm sure that some future version of these compilers will correct the
problem. In the meantime, you must work around the problem. I've
redirected this posting to include comp.lang.c++ -- perhaps someone
there can help you.

----
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: hprieto@sophia.inria.fr (Helene Prieto)
Date: 1999/01/18
Raw View
I have some problems with inheritance and template...
Here is my code :

 #include <iostream.h>
 #define VIEW(a,B) (*static_cast<B*>(&a))
 #define $VIEW(T,C) C<T>
 #define $CATEGORY(S) template<class T> struct S

 $CATEGORY(S) { };
 struct VOID {};

 template<class T> void Prt($VIEW(T,S) & p) { VIEW(p,T).print(); }

 struct A: public S<A> {
   int n;
   A(): n(0) {}
   A(int i): n(i) {}
   void hello() {cout <<"hello ";}
   void print() {cout <<n<<" AA"<<endl;}
 };

 struct B : public S<B>, public A
 {
   B() : A(0) {}
   B(int n) : A(n) {}
   void hello(){cout <<"Hello ";}
   void print() {hello();cout <<"BB"<<endl;}
 };

 main(){
  A a(1); B b(4);

  Prt(a);
  Prt(b);
 }

And this is the result of compilation with g++ or eg++ :

 /*
 Essai.cxx: In function `int main()':
 Essai.cxx:36: no matching function for call to `Prt (B &)'
 *** Error code 1
 make: Warning: Target `Essai.exe' not remade because of errors
 */

So the instance of B is clearly not recognize as a S<B> despite of the
derivation...

So I think it is a problem with g++ and eg++. Does someone can help me for
solution similarly (without virtual methods), or a way to force eg++ to
understand the hierarchy ?


Helene Prieto.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gabriel Dos_Reis <gdosreis@sophia.inria.fr>
Date: 1999/01/19
Raw View
hprieto@sophia.inria.fr (Helene Prieto) writes:


[...]

>
>  main(){
>   A a(1); B b(4);
>
>   Prt(a);
>   Prt(b);
>  }
>

[...]

>
> So I think it is a problem with g++ and eg++. Does someone can help me for
> solution similarly (without virtual methods), or a way to force eg++ to
> understand the hierarchy ?
>

Since b is a S<A> _and_ a S<B>, template argument deduction should
fail, thus the diagnostic message.

OTOH, you may resolve the ambiguity by giving an explicit template
argument

 Ptr<B>(b);

Curiously, EGCS fails to successfully resolve the call. That seems to
me to be a bug.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1999/01/19
Raw View
Helene Prieto<hprieto@sophia.inria.fr> wrote:
>
>I have some problems with inheritance and template...
>Here is my code :
>
> #include <iostream.h>
> #define VIEW(a,B) (*static_cast<B*>(&a))
> #define $VIEW(T,C) C<T>
> #define $CATEGORY(S) template<class T> struct S
>
> $CATEGORY(S) { };
> struct VOID {};
>
> template<class T> void Prt($VIEW(T,S) & p) { VIEW(p,T).print(); }

This is bad code.  Expanding the macros (why use them, if they
cause confusion?), we have

 template<class T> void Prt(S<T> & p) { T<p>.print(); }

"T<p>" is meaningless.  (Maybe you should go down the hall and visit
Gabriel Dos Reis, also at INRIA.)  I haven't looked at the rest of
the code posted, there may be other problems in it.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]