Name Resolution for Class Derived from Template Argument

Discussion in 'C++' started by montyshasta@yahoo.com, Nov 27, 2006.

  1. Guest

    Take this code as a base case, it compiles successfully:

    struct R
    {
    int i;
    };
    class S : public R
    {
    void F(void) {i = 0;}
    };
    S s;

    Switch to deriving S from R as a template argument and the behavior
    varies by compiler:

    struct R
    {
    int i;
    };
    template <class T>
    class S : public T
    {
    void F(void) {i = 0;}
    };
    S<R> s;

    Visual Studio succeeds building this. GCC fails with "error: 'i'
    was not declared in this scope". ICC (Intel's compiler) succeeds with
    that instantiation but fails if you attempt an explicit template
    instantiation, like:
    template class S<R>;

    GCC and ICC succeed if you switch to a qualified reference to i:
    void F(void) {T::i = 0;}

    Is the way namespaces are inherited from base classes supposed to work
    differently if those base classes are template arguments or is
    something wrong with the compilers?
     
    , Nov 27, 2006
    #1
    1. Advertising

  2. Ondra Holub Guest

    napsal:
    > Take this code as a base case, it compiles successfully:
    >
    > struct R
    > {
    > int i;
    > };
    > class S : public R
    > {
    > void F(void) {i = 0;}
    > };
    > S s;
    >
    > Switch to deriving S from R as a template argument and the behavior
    > varies by compiler:
    >
    > struct R
    > {
    > int i;
    > };
    > template <class T>
    > class S : public T
    > {
    > void F(void) {i = 0;}
    > };
    > S<R> s;
    >
    > Visual Studio succeeds building this. GCC fails with "error: 'i'
    > was not declared in this scope". ICC (Intel's compiler) succeeds with
    > that instantiation but fails if you attempt an explicit template
    > instantiation, like:
    > template class S<R>;
    >
    > GCC and ICC succeed if you switch to a qualified reference to i:
    > void F(void) {T::i = 0;}
    >
    > Is the way namespaces are inherited from base classes supposed to work
    > differently if those base classes are template arguments or is
    > something wrong with the compilers?


    I think that behaviour of gcc is correct. When class is derived from
    class defined by template parameter, you cannot access parent's members
    with direct name, you have to write either T::member or this->member.
     
    Ondra Holub, Nov 27, 2006
    #2
    1. Advertising

  3. Marcus Kwok Guest

    wrote:
    > Switch to deriving S from R as a template argument and the behavior
    > varies by compiler:
    >
    > struct R
    > {
    > int i;
    > };
    > template <class T>
    > class S : public T
    > {
    > void F(void) {i = 0;}
    > };
    > S<R> s;
    >
    > Visual Studio succeeds building this. GCC fails with "error: 'i'
    > was not declared in this scope". ICC (Intel's compiler) succeeds with
    > that instantiation but fails if you attempt an explicit template
    > instantiation, like:
    > template class S<R>;
    >
    > GCC and ICC succeed if you switch to a qualified reference to i:
    > void F(void) {T::i = 0;}
    >
    > Is the way namespaces are inherited from base classes supposed to work
    > differently if those base classes are template arguments or is
    > something wrong with the compilers?


    See the FAQ (and the one following it too):
    http://www.parashift.com/c -faq-lite/templates.html#faq-35.19

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Nov 27, 2006
    #3
    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. Matt Graham
    Replies:
    0
    Views:
    578
    Matt Graham
    Jul 21, 2003
  2. Replies:
    1
    Views:
    425
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    413
    Victor Bazarov
    May 23, 2007
  4. Replies:
    7
    Views:
    467
    Adam Nielsen
    Sep 28, 2007
  5. amkg
    Replies:
    2
    Views:
    540
Loading...

Share This Page