C++ template

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Unkown to Xnntp

    C++ template

    Hello,

    The following code works (it compiles ok with "g++ test.c"):

    +++++++++++++++ +++++++++++++++ ++++++++++
    template <class Valueclass Test1 {
    public:
    int a;
    };

    class Test2 : public Test1<int{
    public:
    void f() {
    a = 2;
    }
    };
    +++++++++++++++ +++++++++++++++ ++++++++++


    However, the following code does not compile:
    +++++++++++++++ +++++++++++++++ ++++++++++
    template <class Valueclass Test1 {
    public:
    int a;
    };

    template <class Valueclass Test2 : public Test1<Value{
    public:
    void f() {
    a = 2;
    }
    };
    +++++++++++++++ +++++++++++++++ ++++++++++
    the compiler (g++-4.0.1) says:
    test.c: error: a was not declared in this scope


    What is the problem?
    ps: the code above does not do anything interesting.
    It is just here to illustrate the problem that I currently find while compiling
    my other real program.


    Many thanks,
    DAvid

  • kwikius

    #2
    Re: C++ template

    Unkown to Xnntp wrote:
    Hello,
    <...>

    template <class Valueclass Test2 : public Test1<Value{
    public:
    void f() {
    Test1<Value>:: a = 2;
    }
    };

    Think its something like the above. Because you inherit from template
    you need to be explicit about base . Something to do with
    specialisations etc where in specialisaion compiler cant assume same as
    default.

    Untested in gcc ( orig works OK in VC8)

    regards
    Andy Little

    Comment

    • Pascal J. Bourguignon

      #3
      Re: C++ template

      Unkown to Xnntp <unkown@xnntpwr ites:
      However, the following code does not compile:
      +++++++++++++++ +++++++++++++++ ++++++++++
      template <class Valueclass Test1 {
      public:
      int a;
      };
      template <class Valueclass Test2 : public Test1<Value{
      public:
      void f() {
      a = 2;
      }
      };
      +++++++++++++++ +++++++++++++++ ++++++++++
      the compiler (g++-4.0.1) says:
      test.c: error: .a. was not declared in this scope
      >
      What is the problem?
      With this->a=2; it works.

      --
      __Pascal Bourguignon__

      Comment

      • aman.c++

        #4
        Re: C++ template

        On Aug 6, 4:06 pm, Unkown to Xnntp <unkown@xnntpwr ote:
        template <class Valueclass Test1 {
        public:
        int a;
        >
        };
        >
        template <class Valueclass Test2 : public Test1<Value{
        public:
        void f() {
        a = 2;
        }};
        the compiler (g++-4.0.1) says:
        test.c: error: a was not declared in this scope
        What is the problem
        Hi,
        The problem is that Test1<Valueis a dependent base class and the
        variable a is a non-dependent name. Compilers that use 2 phase lookup
        lookup the non-dependent names early but the dependent names can't be
        resolved when parsing templates. The are resolved at instantiation (ph
        2). Hence the name a is ambiguous.

        To resolve this you could make the name "a" also dependent using
        either of:

        Test1<Value::a = 2;
        this->a = 2;

        For your case either line will work fine but there are situations when
        one might be preferred over the other.

        regards,
        Aman Angrish

        Comment

        • David Portabella

          #5
          Re: C++ template

          "aman.c++" <aman.cpp@gmail .comwrote:
          ...
          Test1<Value::a = 2;
          this->a = 2;
          Thanks everybody for your answers and explanations!

          DAvid


          Comment

          • David Portabella

            #6
            Re: C++ template

            "aman.c++" <aman.cpp@gmail .comwrote:
            On Aug 6, 4:06 pm, Unkown to Xnntp <unkown@xnntpwr ote:
            >template <class Valueclass Test1 {
            > public:
            > int a;
            >>
            >};
            >>
            >template <class Valueclass Test2 : public Test1<Value{
            >public:
            > void f() {
            > a = 2;
            > }};
            >the compiler (g++-4.0.1) says:
            >test.c: error: a was not declared in this scope
            >What is the problem
            Hi,
            The problem is that Test1<Valueis a dependent base class and the
            variable a is a non-dependent name. Compilers that use 2 phase lookup
            lookup the non-dependent names early but the dependent names can't be
            resolved when parsing templates. The are resolved at instantiation (ph
            2). Hence the name a is ambiguous.
            >
            To resolve this you could make the name "a" also dependent using
            either of:
            >
            Test1<Value::a = 2;
            this->a = 2;
            >
            For your case either line will work fine but there are situations when
            one might be preferred over the other.
            >
            regards,
            Aman Angrish

            One more question,

            is there a difference between these two possibilities in terms of
            execution time?
            or, once compiled, they look exactly the same?


            Best regards,
            DAvid


            Comment

            • Juha Nieminen

              #7
              Re: C++ template

              Unkown to Xnntp wrote:
              template <class Valueclass Test2 : public Test1<Value{
              Hate to nitpick about style, but newlines *are* allowed to be used in
              source code for clarity.

              Comment

              • James Kanze

                #8
                Re: C++ template

                On Aug 6, 2:28 pm, "aman.c++" <aman....@gmail .comwrote:
                On Aug 6, 4:06 pm, Unkown to Xnntp <unkown@xnntpwr ote:
                template <class Valueclass Test1 {
                public:
                int a;
                >
                };
                template <class Valueclass Test2 : public Test1<Value{
                public:
                void f() {
                a = 2;
                }};
                the compiler (g++-4.0.1) says:
                test.c: error: a was not declared in this scope
                What is the problem
                The problem is that Test1<Valueis a dependent base class and
                the variable a is a non-dependent name.
                As written, the compiler treats it as non-dependent. The author
                of the code obviously wanted it to be dependent.
                Compilers that use 2 phase lookup
                In other words, compilers that conform to the C++ standard.
                lookup the non-dependent names early but the dependent names
                can't be resolved when parsing templates. The are resolved at
                instantiation (ph 2). Hence the name a is ambiguous.
                Not quite. First, the name isn't ambiguous; it isn't found,
                because non-dependent lookup occurs at the point where the
                template is defined, not where it is instantiated, and doesn't
                take dependent base classes into account. (Note that if he'd
                derived from Test1<int>, there'd be no problem.)

                Dependent names are more complicated, and are searched both in
                the context where the template was defined, and in the context
                where it was instantiated, but only using ADL in the latter
                case.
                To resolve this you could make the name "a" also dependent
                using either of:
                Test1<Value::a = 2;
                this->a = 2;
                Yes. It's a shame that one has to resource to such tricks,
                however, rather than simply being able to say what one means
                (e.g. by explicitly declaring which names should be dependent).
                For your case either line will work fine but there are
                situations when one might be preferred over the other.
                I think that the "this->" is the usual solution, and so it
                should be preferred as long as there are no real reasons to do
                otherwise.

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • James Kanze

                  #9
                  Re: C++ template

                  On Aug 6, 4:18 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                  Unkown to Xnntp wrote:
                  template <class Valueclass Test2 : public Test1<Value{
                  Hate to nitpick about style, but newlines *are* allowed to be
                  used in source code for clarity.
                  I agree, and the above would be three or four lines in my own
                  code. But I think a lot of people compress the code when
                  posting, on the grounds that a ten line posting is more likely
                  to get answers than a twenty line one.

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  • Greg Comeau

                    #10
                    Re: C++ template

                    In article <F16079E2-E4C9-44FD-83AD-ADFD63CEAE67%Xn ntp@beta06.com> ,
                    Unkown to Xnntp <unkown@xnntpwr ote:
                    >... the following code does not compile:
                    >template <class Valueclass Test1 {
                    public:
                    int a;
                    >};
                    >
                    >template <class Valueclass Test2 : public Test1<Value{
                    >public:
                    void f() {
                    a = 2;
                    }
                    >};
                    >the compiler (g++-4.0.1) says:
                    >test.c: error: a was not declared in this scope
                    >
                    >What is the problem?
                    >ps: the code above does not do anything interesting.
                    >It is just here to illustrate the problem that I currently find
                    >while compiling my other real program.

                    --
                    Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
                    Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
                    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                    Comment

                    • Greg Comeau

                      #11
                      Re: C++ template

                      In article <71EE5B57-58A6-4AAA-A496-D62AD486DA2F%da vid.portabella@ gmail.com>,
                      David Portabella <david.portabel la@gmail.comwro te:
                      >"aman.c++" <aman.cpp@gmail .comwrote:
                      >On Aug 6, 4:06 pm, Unkown to Xnntp <unkown@xnntpwr ote:
                      >>template <class Valueclass Test1 {
                      >> public:
                      >> int a;
                      >>>
                      >>};
                      >>>
                      >>template <class Valueclass Test2 : public Test1<Value{
                      >>public:
                      >> void f() {
                      >> a = 2;
                      >> }};
                      >>the compiler (g++-4.0.1) says:
                      >>test.c: error: a was not declared in this scope
                      >>What is the problem
                      >Hi,
                      >The problem is that Test1<Valueis a dependent base class and the
                      >variable a is a non-dependent name. Compilers that use 2 phase lookup
                      >lookup the non-dependent names early but the dependent names can't be
                      >resolved when parsing templates. The are resolved at instantiation (ph
                      >2). Hence the name a is ambiguous.
                      >>
                      >To resolve this you could make the name "a" also dependent using
                      >either of:
                      >>
                      >Test1<Value: :a = 2;
                      >this->a = 2;
                      >>
                      >For your case either line will work fine but there are situations when
                      >one might be preferred over the other.
                      >
                      >is there a difference between these two possibilities in terms of
                      >execution time?
                      >or, once compiled, they look exactly the same?
                      Should be the same. If you will, consider a composite of both
                      as to what is happening in either case.
                      --
                      Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
                      Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
                      World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                      Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                      Comment

                      Working...