accessing members of a templated base class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Harrison

    accessing members of a templated base class

    This code fails to compile on Comeau C++ and VC++ 7.1 (with language
    extensions disabled)

    template <class T>
    struct B
    {
    T b;
    };

    template <class T>
    struct D : B<T>
    {
    void f() { b = 1; }
    };

    int main()
    {
    D<int> x;
    x.f();
    }

    Error messages refer to 'b = 1;' with the message 'undefined identifier b'.
    Substituting this->b for b or making B a non-template class both make the
    code compile.

    What's going on here? I was so surprised when I saw this on VC++ that I
    assumed it was a compiler bug, but apparently not.

    john


  • Victor Bazarov

    #2
    Re: accessing members of a templated base class

    "John Harrison" <john_andronicu s@hotmail.com> wrote...[color=blue]
    > This code fails to compile on Comeau C++ and VC++ 7.1 (with language
    > extensions disabled)
    >
    > template <class T>
    > struct B
    > {
    > T b;
    > };
    >
    > template <class T>
    > struct D : B<T>
    > {
    > void f() { b = 1; }
    > };
    >
    > int main()
    > {
    > D<int> x;
    > x.f();
    > }
    >
    > Error messages refer to 'b = 1;' with the message 'undefined identifier[/color]
    b'.[color=blue]
    > Substituting this->b for b or making B a non-template class both make the
    > code compile.
    >
    > What's going on here? I was so surprised when I saw this on VC++ that I
    > assumed it was a compiler bug, but apparently not.[/color]

    Why do you say "apparently not"? I cannot find anything in the Standard
    that would make the look-up of name 'b' in D<T>::f() fail. As far as the
    Standard goes, the usual rules of 10.2 should apply. The base class has
    to be searched if 'b' is not found in D definition.

    14.6/8 says "When looking for the declaration of a name used in a template
    definition, the usual lookup rules (3.4.1, 3.4.2) are used for nondependent
    names."

    3.4.1/8 says "A name used in the definition of a function that is a member
    function (9.3)29) of class X shall be declared in one of the following
    ways:
    - before its use in the block in which it is used or in an enclosing block
    (6.3), or
    - shall be a member of class X or be a member of a base class of X (10.2),
    or ..."

    So, "shall be a member of class X or be a member of a base class of X"
    should do it. 'b' is a member of the base class.

    Well, at least that's how I read it. Perhaps Greg Comeau or other
    knowledgeable people will enlighten us both.

    Victor


    Comment

    • Victor Bazarov

      #3
      Re: accessing members of a templated base class

      "John Harrison" <john_andronicu s@hotmail.com> wrote...[color=blue]
      > This code fails to compile on Comeau C++ and VC++ 7.1 (with language
      > extensions disabled)
      >
      > template <class T>
      > struct B
      > {
      > T b;
      > };
      >
      > template <class T>
      > struct D : B<T>
      > {
      > void f() { b = 1; }
      > };
      >
      > int main()
      > {
      > D<int> x;
      > x.f();
      > }
      >
      > Error messages refer to 'b = 1;' with the message 'undefined identifier[/color]
      b'.[color=blue]
      > Substituting this->b for b or making B a non-template class both make the
      > code compile.
      >
      > What's going on here? I was so surprised when I saw this on VC++ that I
      > assumed it was a compiler bug, but apparently not.[/color]

      Why do you say "apparently not"? I cannot find anything in the Standard
      that would make the look-up of name 'b' in D<T>::f() fail. As far as the
      Standard goes, the usual rules of 10.2 should apply. The base class has
      to be searched if 'b' is not found in D definition.

      14.6/8 says "When looking for the declaration of a name used in a template
      definition, the usual lookup rules (3.4.1, 3.4.2) are used for nondependent
      names."

      3.4.1/8 says "A name used in the definition of a function that is a member
      function (9.3)29) of class X shall be declared in one of the following
      ways:
      - before its use in the block in which it is used or in an enclosing block
      (6.3), or
      - shall be a member of class X or be a member of a base class of X (10.2),
      or ..."

      So, "shall be a member of class X or be a member of a base class of X"
      should do it. 'b' is a member of the base class.

      Well, at least that's how I read it. Perhaps Greg Comeau or other
      knowledgeable people will enlighten us both.

      Victor


      Comment

      • Alf P. Steinbach

        #4
        Re: accessing members of a templated base class

        On Sun, 10 Aug 2003 14:13:59 +0100, "John Harrison" <john_andronicu s@hotmail.com> wrote:
        [color=blue]
        >This code fails to compile on Comeau C++ and VC++ 7.1 (with language
        >extensions disabled)
        >
        >template <class T>
        >struct B
        >{
        > T b;
        >};
        >
        >template <class T>
        >struct D : B<T>
        >{
        > void f() { b = 1; }
        >};
        >
        >int main()
        >{
        > D<int> x;
        > x.f();
        >}
        >
        >Error messages refer to 'b = 1;' with the message 'undefined identifier b'.
        >Substituting this->b for b or making B a non-template class both make the
        >code compile.[/color]

        Am I glad that I haven't even checked whether I _have_ VC 7.1... ;-)

        It compiles just fine with 7.0, with and without language extensions
        disabled (/Za).


        [color=blue]
        >What's going on here? I was so surprised when I saw this on VC++ that I
        >assumed it was a compiler bug, but apparently not.[/color]

        Apparently it is a compiler bug.

        At least, I agree with Victor that the usual look-up rules should apply.

        Comment

        • Alf P. Steinbach

          #5
          Re: accessing members of a templated base class

          On Sun, 10 Aug 2003 14:13:59 +0100, "John Harrison" <john_andronicu s@hotmail.com> wrote:
          [color=blue]
          >This code fails to compile on Comeau C++ and VC++ 7.1 (with language
          >extensions disabled)
          >
          >template <class T>
          >struct B
          >{
          > T b;
          >};
          >
          >template <class T>
          >struct D : B<T>
          >{
          > void f() { b = 1; }
          >};
          >
          >int main()
          >{
          > D<int> x;
          > x.f();
          >}
          >
          >Error messages refer to 'b = 1;' with the message 'undefined identifier b'.
          >Substituting this->b for b or making B a non-template class both make the
          >code compile.[/color]

          Am I glad that I haven't even checked whether I _have_ VC 7.1... ;-)

          It compiles just fine with 7.0, with and without language extensions
          disabled (/Za).


          [color=blue]
          >What's going on here? I was so surprised when I saw this on VC++ that I
          >assumed it was a compiler bug, but apparently not.[/color]

          Apparently it is a compiler bug.

          At least, I agree with Victor that the usual look-up rules should apply.

          Comment

          • Pete Becker

            #6
            Re: accessing members of a templated base class

            "Alf P. Steinbach" wrote:[color=blue]
            >
            > Am I glad that I haven't even checked whether I _have_ VC 7.1... ;-)[/color]

            Are you also glad that you haven't checked whether you have the EDG
            front end? <g> It rejects this code, too.
            [color=blue]
            >
            > It compiles just fine with 7.0, with and without language extensions
            > disabled (/Za).
            >[color=green]
            > >What's going on here? I was so surprised when I saw this on VC++ that I
            > >assumed it was a compiler bug, but apparently not.[/color]
            >
            > Apparently it is a compiler bug.
            >
            > At least, I agree with Victor that the usual look-up rules should apply.[/color]

            Since EDG rejects it, I suspect that the analysis is wrong. I don't
            claim to understand two-phase lookup, but it looks to me like 'b' is a
            dependent name. After all, its meaning depends on the defintion of B<T>,
            and B<T> can be specialized for various T's. During phase one it has to
            be explicitly qualified, in order to tell the compiler that not having a
            'b' in B<T> is an error.

            --

            Pete Becker
            Dinkumware, Ltd. (http://www.dinkumware.com)

            Comment

            • Pete Becker

              #7
              Re: accessing members of a templated base class

              "Alf P. Steinbach" wrote:[color=blue]
              >
              > Am I glad that I haven't even checked whether I _have_ VC 7.1... ;-)[/color]

              Are you also glad that you haven't checked whether you have the EDG
              front end? <g> It rejects this code, too.
              [color=blue]
              >
              > It compiles just fine with 7.0, with and without language extensions
              > disabled (/Za).
              >[color=green]
              > >What's going on here? I was so surprised when I saw this on VC++ that I
              > >assumed it was a compiler bug, but apparently not.[/color]
              >
              > Apparently it is a compiler bug.
              >
              > At least, I agree with Victor that the usual look-up rules should apply.[/color]

              Since EDG rejects it, I suspect that the analysis is wrong. I don't
              claim to understand two-phase lookup, but it looks to me like 'b' is a
              dependent name. After all, its meaning depends on the defintion of B<T>,
              and B<T> can be specialized for various T's. During phase one it has to
              be explicitly qualified, in order to tell the compiler that not having a
              'b' in B<T> is an error.

              --

              Pete Becker
              Dinkumware, Ltd. (http://www.dinkumware.com)

              Comment

              • Alf P. Steinbach

                #8
                Re: accessing members of a templated base class

                On Sun, 10 Aug 2003 10:12:08 -0400, Pete Becker <petebecker@acm .org> wrote:
                [color=blue]
                >"Alf P. Steinbach" wrote:[color=green]
                >>
                >> Am I glad that I haven't even checked whether I _have_ VC 7.1... ;-)[/color]
                >
                >Are you also glad that you haven't checked whether you have the EDG
                >front end? <g> It rejects this code, too.[/color]

                I'm pretty sure that I don't have that, since I don't have Comeau,
                unless it's used by Visual C++ 7.1.


                [color=blue][color=green]
                >> It compiles just fine with 7.0, with and without language extensions
                >> disabled (/Za).
                >>[color=darkred]
                >> >What's going on here? I was so surprised when I saw this on VC++ that I
                >> >assumed it was a compiler bug, but apparently not.[/color]
                >>
                >> Apparently it is a compiler bug.
                >>
                >> At least, I agree with Victor that the usual look-up rules should apply.[/color]
                >
                >Since EDG rejects it, I suspect that the analysis is wrong.[/color]

                EDG lists Comeau Computing as one of the compilers (or at least, companies)
                that use their front-end.

                If that's correct that means it's still just two compilers (or compiler
                parts) that reject the code: Visual C++ 7.1 and Comeau.

                If Visual C++ 7.1 also uses the EDG front-end then it's just one.

                Comment

                • Alf P. Steinbach

                  #9
                  Re: accessing members of a templated base class

                  On Sun, 10 Aug 2003 10:12:08 -0400, Pete Becker <petebecker@acm .org> wrote:
                  [color=blue]
                  >"Alf P. Steinbach" wrote:[color=green]
                  >>
                  >> Am I glad that I haven't even checked whether I _have_ VC 7.1... ;-)[/color]
                  >
                  >Are you also glad that you haven't checked whether you have the EDG
                  >front end? <g> It rejects this code, too.[/color]

                  I'm pretty sure that I don't have that, since I don't have Comeau,
                  unless it's used by Visual C++ 7.1.


                  [color=blue][color=green]
                  >> It compiles just fine with 7.0, with and without language extensions
                  >> disabled (/Za).
                  >>[color=darkred]
                  >> >What's going on here? I was so surprised when I saw this on VC++ that I
                  >> >assumed it was a compiler bug, but apparently not.[/color]
                  >>
                  >> Apparently it is a compiler bug.
                  >>
                  >> At least, I agree with Victor that the usual look-up rules should apply.[/color]
                  >
                  >Since EDG rejects it, I suspect that the analysis is wrong.[/color]

                  EDG lists Comeau Computing as one of the compilers (or at least, companies)
                  that use their front-end.

                  If that's correct that means it's still just two compilers (or compiler
                  parts) that reject the code: Visual C++ 7.1 and Comeau.

                  If Visual C++ 7.1 also uses the EDG front-end then it's just one.

                  Comment

                  • John Harrison

                    #10
                    Re: accessing members of a templated base class

                    > >[color=blue][color=green]
                    > > What's going on here? I was so surprised when I saw this on VC++ that I
                    > > assumed it was a compiler bug, but apparently not.[/color]
                    >
                    > Why do you say "apparently not"?[/color]

                    Because two apparently unrelated compilers produce effectively identical
                    error messages. I hadn't considered Alf's suggestion that VC++ 7.1 might use
                    the EDG front end. However Microsoft aren't on EDG's list of corporate
                    licensees and you might expect them to make a fuss about it if MS were using
                    EDG.

                    john


                    Comment

                    • John Harrison

                      #11
                      Re: accessing members of a templated base class

                      > >[color=blue][color=green]
                      > > What's going on here? I was so surprised when I saw this on VC++ that I
                      > > assumed it was a compiler bug, but apparently not.[/color]
                      >
                      > Why do you say "apparently not"?[/color]

                      Because two apparently unrelated compilers produce effectively identical
                      error messages. I hadn't considered Alf's suggestion that VC++ 7.1 might use
                      the EDG front end. However Microsoft aren't on EDG's list of corporate
                      licensees and you might expect them to make a fuss about it if MS were using
                      EDG.

                      john


                      Comment

                      • Alf P. Steinbach

                        #12
                        Re: accessing members of a templated base class

                        On Sun, 10 Aug 2003 15:47:03 +0100, "John Harrison" <john_andronicu s@hotmail.com> wrote:
                        [color=blue][color=green][color=darkred]
                        >> >
                        >> > What's going on here? I was so surprised when I saw this on VC++ that I
                        >> > assumed it was a compiler bug, but apparently not.[/color]
                        >>
                        >> Why do you say "apparently not"?[/color]
                        >
                        >Because two apparently unrelated compilers produce effectively identical
                        >error messages. I hadn't considered Alf's suggestion that VC++ 7.1 might use
                        >the EDG front end. However Microsoft aren't on EDG's list of corporate
                        >licensees and you might expect them to make a fuss about it if MS were using
                        >EDG.[/color]

                        Only about 50% of the licensees are on the public list.

                        Comment

                        • Alf P. Steinbach

                          #13
                          Re: accessing members of a templated base class

                          On Sun, 10 Aug 2003 15:47:03 +0100, "John Harrison" <john_andronicu s@hotmail.com> wrote:
                          [color=blue][color=green][color=darkred]
                          >> >
                          >> > What's going on here? I was so surprised when I saw this on VC++ that I
                          >> > assumed it was a compiler bug, but apparently not.[/color]
                          >>
                          >> Why do you say "apparently not"?[/color]
                          >
                          >Because two apparently unrelated compilers produce effectively identical
                          >error messages. I hadn't considered Alf's suggestion that VC++ 7.1 might use
                          >the EDG front end. However Microsoft aren't on EDG's list of corporate
                          >licensees and you might expect them to make a fuss about it if MS were using
                          >EDG.[/color]

                          Only about 50% of the licensees are on the public list.

                          Comment

                          • Pete Becker

                            #14
                            Re: accessing members of a templated base class

                            "Alf P. Steinbach" wrote:[color=blue]
                            >
                            > On Sun, 10 Aug 2003 10:12:08 -0400, Pete Becker <petebecker@acm .org> wrote:
                            >[color=green]
                            > >"Alf P. Steinbach" wrote:[color=darkred]
                            > >>
                            > >> Am I glad that I haven't even checked whether I _have_ VC 7.1... ;-)[/color]
                            > >
                            > >Are you also glad that you haven't checked whether you have the EDG
                            > >front end? <g> It rejects this code, too.[/color]
                            >
                            > I'm pretty sure that I don't have that, since I don't have Comeau,
                            > unless it's used by Visual C++ 7.1.
                            >[color=green][color=darkred]
                            > >> It compiles just fine with 7.0, with and without language extensions
                            > >> disabled (/Za).
                            > >>
                            > >> >What's going on here? I was so surprised when I saw this on VC++ that I
                            > >> >assumed it was a compiler bug, but apparently not.
                            > >>
                            > >> Apparently it is a compiler bug.
                            > >>
                            > >> At least, I agree with Victor that the usual look-up rules should apply.[/color]
                            > >
                            > >Since EDG rejects it, I suspect that the analysis is wrong.[/color]
                            >
                            > EDG lists Comeau Computing as one of the compilers (or at least, companies)
                            > that use their front-end.
                            >
                            > If that's correct that means it's still just two compilers (or compiler
                            > parts) that reject the code: Visual C++ 7.1 and Comeau.
                            >
                            > If Visual C++ 7.1 also uses the EDG front-end then it's just one.[/color]

                            Visual C++ 7.1 does not use the EDG front end. Even if it did, counting
                            compilers doesn't tell you much. EDG is usually right.

                            --

                            Pete Becker
                            Dinkumware, Ltd. (http://www.dinkumware.com)

                            Comment

                            • Kristoffer Vinther

                              #15
                              Re: accessing members of a templated base class

                              "Pete Becker" <petebecker@acm .org> wrote in message
                              news:3F3652B8.D 536748A@acm.org ...[color=blue]
                              > "Alf P. Steinbach" wrote:[color=green]
                              > >
                              > > Am I glad that I haven't even checked whether I _have_ VC 7.1... ;-)[/color]
                              >
                              > Are you also glad that you haven't checked whether you have the EDG
                              > front end? <g> It rejects this code, too.
                              >[color=green]
                              > >
                              > > It compiles just fine with 7.0, with and without language extensions
                              > > disabled (/Za).
                              > >[color=darkred]
                              > > >What's going on here? I was so surprised when I saw this on VC++ that
                              > > > I assumed it was a compiler bug, but apparently not.[/color]
                              > >
                              > > Apparently it is a compiler bug.
                              > >
                              > > At least, I agree with Victor that the usual look-up rules should
                              > > apply.[/color]
                              >
                              > Since EDG rejects it, I suspect that the analysis is wrong. I don't
                              > claim to understand two-phase lookup, but it looks to me like 'b' is a
                              > dependent name.[/color]

                              Exactly. Use

                              template <class T>
                              struct B
                              {
                              T b;
                              };

                              template <class T>
                              struct D : B<T>
                              {
                              void f() { B<T>::b = 1; }
                              };

                              or perhaps

                              template <class T>
                              struct B
                              {
                              T b;
                              };

                              template <class T>
                              struct D : B<T>
                              {
                              using B<T>::b;
                              void f() { b = 1; }
                              };

                              if you'll be using B's b in many places in D (note, if B's b was protected,
                              you should probably also make the using decl. protected).

                              /kv


                              Comment

                              Working...