class vs. typename

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Malkis

    class vs. typename

    What's the semantical/syntactical difference between two keywords
    "class" and "typename" (apart from different spelling)?

    --
    Best regards,
    Alex.

    PS. To email me, remove "loeschedie s" from the email address given.
  • John Harrison

    #2
    Re: class vs. typename


    "Alexander Malkis" <alexloeschedie smalk@stone.cs. uni-sb.de> wrote in message
    news:c4uupa$cbc $1@hades.rz.uni-saarland.de...[color=blue]
    > What's the semantical/syntactical difference between two keywords
    > "class" and "typename" (apart from different spelling)?
    >[/color]

    None at all.

    I think typename was introduced because not all template parameters are
    classes.

    john


    Comment

    • John Harrison

      #3
      Re: class vs. typename


      "Alexander Malkis" <alexloeschedie smalk@stone.cs. uni-sb.de> wrote in message
      news:c4uupa$cbc $1@hades.rz.uni-saarland.de...[color=blue]
      > What's the semantical/syntactical difference between two keywords
      > "class" and "typename" (apart from different spelling)?
      >[/color]

      None at all.

      I think typename was introduced because not all template parameters are
      classes.

      john


      Comment

      • Steven T. Hatton

        #4
        Re: class vs. typename

        John Harrison wrote:
        [color=blue]
        >
        > "Alexander Malkis" <alexloeschedie smalk@stone.cs. uni-sb.de> wrote in
        > message news:c4uupa$cbc $1@hades.rz.uni-saarland.de...[color=green]
        >> What's the semantical/syntactical difference between two keywords
        >> "class" and "typename" (apart from different spelling)?
        >>[/color]
        >
        > None at all.
        >
        > I think typename was introduced because not all template parameters are
        > classes.
        >
        > john[/color]
        I believe we need to restrict the discussion to template parameters in order
        for that to hold. This is from the C++ Standard:

        "There is no semantic difference between class and typename in a
        template-parameter."

        Just being pedantic.
        --
        p->m == (*p).m == p[0].m

        Modernize your infrastructure with SUSE Linux Enterprise servers, cloud technology for IaaS, and SUSE's software-defined...

        Mozilla is the not-for-profit behind the lightning fast Firefox browser. We put people over profit to give everyone more power online.

        Comment

        • Steven T. Hatton

          #5
          Re: class vs. typename

          John Harrison wrote:
          [color=blue]
          >
          > "Alexander Malkis" <alexloeschedie smalk@stone.cs. uni-sb.de> wrote in
          > message news:c4uupa$cbc $1@hades.rz.uni-saarland.de...[color=green]
          >> What's the semantical/syntactical difference between two keywords
          >> "class" and "typename" (apart from different spelling)?
          >>[/color]
          >
          > None at all.
          >
          > I think typename was introduced because not all template parameters are
          > classes.
          >
          > john[/color]
          I believe we need to restrict the discussion to template parameters in order
          for that to hold. This is from the C++ Standard:

          "There is no semantic difference between class and typename in a
          template-parameter."

          Just being pedantic.
          --
          p->m == (*p).m == p[0].m

          Modernize your infrastructure with SUSE Linux Enterprise servers, cloud technology for IaaS, and SUSE's software-defined...

          Mozilla is the not-for-profit behind the lightning fast Firefox browser. We put people over profit to give everyone more power online.

          Comment

          • Leor Zolman

            #6
            Re: class vs. typename

            On Tue, 6 Apr 2004 20:45:09 +0100, "John Harrison"
            <john_andronicu s@hotmail.com> wrote:
            [color=blue]
            >
            >"Alexander Malkis" <alexloeschedie smalk@stone.cs. uni-sb.de> wrote in message
            >news:c4uupa$cb c$1@hades.rz.un i-saarland.de...[color=green]
            >> What's the semantical/syntactical difference between two keywords
            >> "class" and "typename" (apart from different spelling)?
            >>[/color]
            >
            >None at all.
            >
            >I think typename was introduced because not all template parameters are
            >classes.[/color]
            Actually it was introduced in order to inform the compiler that a name
            dependent upon a template parameter is a type; Standard C++ assumes that it
            is not in that context unless the typename keyword is used. However, many
            existing implementations allow the "typename" to be omitted in that
            context, some [like gcc] at least warning you about the assumptions being
            made.

            I think they chose to allow "typename" instead of "class" for template
            parameters after the fact, simply as a way to document the nature of the
            type parameters expected. This is one of stylistic issues for which the
            "pendulum" still seems to be swinging...
            -leor
            [color=blue]
            >
            >john
            >[/color]

            --
            Leor Zolman --- BD Software --- www.bdsoft.com
            On-Site Training in C/C++, Java, Perl and Unix
            C++ users: Download BD Software's free STL Error Message Decryptor at:
            An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

            Comment

            • Leor Zolman

              #7
              Re: class vs. typename

              On Tue, 6 Apr 2004 20:45:09 +0100, "John Harrison"
              <john_andronicu s@hotmail.com> wrote:
              [color=blue]
              >
              >"Alexander Malkis" <alexloeschedie smalk@stone.cs. uni-sb.de> wrote in message
              >news:c4uupa$cb c$1@hades.rz.un i-saarland.de...[color=green]
              >> What's the semantical/syntactical difference between two keywords
              >> "class" and "typename" (apart from different spelling)?
              >>[/color]
              >
              >None at all.
              >
              >I think typename was introduced because not all template parameters are
              >classes.[/color]
              Actually it was introduced in order to inform the compiler that a name
              dependent upon a template parameter is a type; Standard C++ assumes that it
              is not in that context unless the typename keyword is used. However, many
              existing implementations allow the "typename" to be omitted in that
              context, some [like gcc] at least warning you about the assumptions being
              made.

              I think they chose to allow "typename" instead of "class" for template
              parameters after the fact, simply as a way to document the nature of the
              type parameters expected. This is one of stylistic issues for which the
              "pendulum" still seems to be swinging...
              -leor
              [color=blue]
              >
              >john
              >[/color]

              --
              Leor Zolman --- BD Software --- www.bdsoft.com
              On-Site Training in C/C++, Java, Perl and Unix
              C++ users: Download BD Software's free STL Error Message Decryptor at:
              An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

              Comment

              • Asfand Yar Qazi

                #8
                Re: class vs. typename

                Alexander Malkis wrote:[color=blue]
                > What's the semantical/syntactical difference between two keywords
                > "class" and "typename" (apart from different spelling)?
                >[/color]

                "class" has less characters in it than "typename", so saves more trees.

                :-)


                --

                Comment

                • Asfand Yar Qazi

                  #9
                  Re: class vs. typename

                  Alexander Malkis wrote:[color=blue]
                  > What's the semantical/syntactical difference between two keywords
                  > "class" and "typename" (apart from different spelling)?
                  >[/color]

                  "class" has less characters in it than "typename", so saves more trees.

                  :-)


                  --

                  Comment

                  • Steven T. Hatton

                    #10
                    Re: class vs. typename

                    Alexander Malkis wrote:
                    [color=blue]
                    > What's the semantical/syntactical difference between two keywords
                    > "class" and "typename" (apart from different spelling)?
                    >[/color]
                    I don't know if anybody mentioned this, I just learned it. It is discussed
                    in TC++PL(SE) Appendix C.13.5. Stroustrup explains it as a way of
                    disambiguating statements in template declarations. For example


                    template<class C> void h(C& v)
                    {
                    typename C::iterator i = v.begin();
                    }

                    --
                    STH
                    Hatton's Law: "There is only One inviolable Law"
                    KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
                    Mozilla: http://www.mozilla.org

                    Comment

                    • NPC

                      #11
                      Re: class vs. typename


                      "Steven T. Hatton" <susudata@setid ava.kushan.aa> wrote in message
                      news:MpKdnaADE6 a_NOTdRVn-jA@speakeasy.ne t...[color=blue]
                      > Alexander Malkis wrote:
                      >[color=green]
                      > > What's the semantical/syntactical difference between two keywords
                      > > "class" and "typename" (apart from different spelling)?
                      > >[/color]
                      > I don't know if anybody mentioned this, I just learned it. It is[/color]
                      discussed[color=blue]
                      > in TC++PL(SE) Appendix C.13.5. Stroustrup explains it as a way of
                      > disambiguating statements in template declarations. For example
                      >
                      >
                      > template<class C> void h(C& v)
                      > {
                      > typename C::iterator i = v.begin();
                      > }
                      >
                      > --
                      > STH
                      > Hatton's Law: "There is only One inviolable Law"
                      > KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
                      > Mozilla: http://www.mozilla.org[/color]


                      Sometimes compilers need some guidance. Typename is a way of telling the
                      compiler that C::iterator is a C++ Type (as opposed to a C++ function call,
                      for example).
                      You will only see typename used in contexts related to programs using
                      templates.
                      As far as how/when to use typename : use it when inside of a templated
                      class/function and refering to a typedef belonging to the templated
                      parameter. For example, C in you code above is a templated parameter.
                      iterator is a typedef defined in C. Therefore, you must use the typename
                      keyword when declaring an object of that type (or if re-typedef'ing (in C):
                      typedef typename C::iterator IteratorType).

                      NPC


                      Comment

                      Working...