template and typedef

Discussion in 'C++' started by Berardino la Torre, Feb 21, 2008.

  1. Hi All,

    is it possible to do something like this ? :

    template <typename Base>
    class Object : public Base::Interface {
    };

    class IA{
    virtual void print()=0;
    };

    class A : public Object<A>{
    public:
    typedef IA A::Interface;
    void print(){
    }
    };


    int main(){

    return 0;
    }

    thx

    Bera
    Berardino la Torre, Feb 21, 2008
    #1
    1. Advertising

  2. Berardino la Torre wrote:
    > is it possible to do something like this ? :
    >
    > template <typename Base>
    > class Object : public Base::Interface {
    > };
    >
    > class IA{
    > virtual void print()=0;
    > };
    >
    > class A : public Object<A>{
    > public:
    > typedef IA A::Interface;
    > void print(){
    > }
    > };
    >
    >
    > int main(){
    >
    > return 0;
    > }


    What is the intent here? I have hard time figuring out what you
    are trying to accomplish. Perhaps if you would show the _use_ for
    class A or class IA...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 21, 2008
    #2
    1. Advertising

  3. Berardino la Torre

    Kira Yamato Guest

    On 2008-02-21 05:05:38 -0500, Berardino la Torre
    <> said:

    > Hi All,
    >
    > is it possible to do something like this ? :
    >
    > template <typename Base>
    > class Object : public Base::Interface {
    > };


    g++ seems to complain that you cannot inherit from an incomplete type Base.

    This is interesting. Just when you think you've figured out how
    template works, here comes another example showing you how much you
    don't know.

    >
    > class IA{
    > virtual void print()=0;
    > };
    >
    > class A : public Object<A>{
    > public:
    > typedef IA A::Interface;
    > void print(){
    > }
    > };


    Seems like your goal is to have class A inherit from A::Interface. So,
    why not consider the following:

    class IA {};

    class A : public IA
    {
    public:
    typedef IA Interface;
    };

    >
    >
    > int main(){
    >
    > return 0;
    > }


    --

    // kira
    Kira Yamato, Feb 21, 2008
    #3
  4. Berardino la Torre

    Kira Yamato Guest

    On 2008-02-21 10:59:16 -0500, Kira Yamato <> said:

    > On 2008-02-21 05:05:38 -0500, Berardino la Torre
    > <> said:
    >
    >> Hi All,
    >>
    >> is it possible to do something like this ? :
    >>
    >> template <typename Base>
    >> class Object : public Base::Interface {
    >> };

    >
    > g++ seems to complain that you cannot inherit from an incomplete type Base.
    >
    > This is interesting. Just when you think you've figured out how
    > template works, here comes another example showing you how much you
    > don't know.


    Well I suppose it makes sense that this should be an error. It's
    basically a circular definition that could lead to infinite regress.
    For example,

    template<class T>
    class A : public T {};

    class B : public A<B>
    {
    int x;
    };

    But this code expands to

    class B : public B
    {
    int x;
    };

    Hence the infinite regress.

    >
    >>
    >> class IA{
    >> virtual void print()=0;
    >> };
    >>
    >> class A : public Object<A>{
    >> public:
    >> typedef IA A::Interface;
    >> void print(){
    >> }
    >> };

    >
    > Seems like your goal is to have class A inherit from A::Interface. So,
    > why not consider the following:
    >
    > class IA {};
    >
    > class A : public IA
    > {
    > public:
    > typedef IA Interface;
    > };
    >
    >>
    >>
    >> int main(){
    >>
    >> return 0;
    >> }


    --

    // kira
    Kira Yamato, Feb 21, 2008
    #4
  5. Berardino la Torre

    Jeff Schwab Guest

    Berardino la Torre wrote:
    > Hi All,
    >
    > is it possible to do something like this ? :


    [Summarized for brevity:]

    template <typename Base>
    struct Object: Base::Interface { };

    struct IA { virtual void print() =0; };

    struct A: Object<A> {
    typedef IA A::Interface;
    void print() { }
    };


    You're trying to derive a class (indirectly) from a type defined within
    that class. I don't know of any direct way to do that. You can instead
    move the type definition outside the class itself, e.g. into a traits class:

    template<typename T>
    struct Interface;

    template <typename Base>
    struct Object: Interface<Base>::Type { };

    struct IA { virtual void print() =0; };

    struct A;

    template<>
    struct Interface<A> {
    typedef IA Type;
    };

    struct A: Object<A> {
    void print() { }
    };
    Jeff Schwab, Feb 21, 2008
    #5
  6. Berardino la Torre

    James Kanze Guest

    On Feb 21, 8:57 pm, Jeff Schwab <> wrote:
    > Berardino la Torre wrote:
    > > is it possible to do something like this ? :


    [Berardino's original post isn't showing up here, but...]

    > [Summarized for brevity:]


    > template <typename Base>
    > struct Object: Base::Interface { };


    This line isn't legal C++. What it almost certainly should be
    is:

    struct Object : typename Base::Interface {} ;


    > struct IA { virtual void print() =0; };


    > struct A: Object<A> {
    > typedef IA A::Interface;
    > void print() { }
    > };


    > You're trying to derive a class (indirectly) from a type
    > defined within that class.


    Which is impossible, templates or not.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Feb 22, 2008
    #6
  7. Berardino la Torre

    gpderetta Guest

    On Feb 22, 11:24 am, James Kanze <> wrote:
    > On Feb 21, 8:57 pm, Jeff Schwab <> wrote:
    >
    > > Berardino la Torre wrote:
    > > > is it possible to do something like this ? :

    >
    > [Berardino's original post isn't showing up here, but...]
    >
    > > [Summarized for brevity:]
    > > template <typename Base>
    > > struct Object: Base::Interface { };

    >
    > This line isn't legal C++.


    This is actually legal and correct. In that context you can only have
    a typename,
    so you do not need to add the 'typename' keyword. There is no
    ambiguity.

    > What it almost certainly should be
    > is:
    >
    > struct Object : typename Base::Interface {} ;


    And this is not. Both gcc and comeau online complain that typename is
    not allowed in this context.

    As if the rules weren't complicated enough... :).
    IIRC the next standard will relax the rule
    and allow (but not require) a typename there.

    HTH,

    --
    gpd
    gpderetta, Feb 22, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arkadiy Vertleyb
    Replies:
    5
    Views:
    2,327
    Arkadiy Vertleyb
    Sep 27, 2003
  2. marco
    Replies:
    2
    Views:
    351
    marco
    Sep 2, 2004
  3. Adam Nielsen
    Replies:
    3
    Views:
    407
  4. oor
    Replies:
    0
    Views:
    1,346
  5. Replies:
    1
    Views:
    604
Loading...

Share This Page