reference-to-const

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

    reference-to-const

    hi all,

    could anyone explain the differences between:


    template<typena me T> // let's suppose that T could be a user - defined class
    foo(T const & p);

    and

    template<typena me T>
    foo(const T& p);

    in terms of use, efficiency, use of temporary objects and so on...

    thanks
  • Andrey Tarasevich

    #2
    Re: reference-to-const

    karl wrote:[color=blue]
    > ...
    > template<typena me T> // let's suppose that T could be a user - defined class
    > foo(T const & p);
    >
    > and
    >
    > template<typena me T>
    > foo(const T& p);
    >
    > in terms of use, efficiency, use of temporary objects and so on...
    > ...[/color]

    No difference at all.

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Mark Kerns

      #3
      Re: reference-to-const

      > > template<typena me T> // let's suppose that T could be a user -
      defined class[color=blue][color=green]
      > > foo(T const & p);
      > >
      > > and
      > >
      > > template<typena me T>
      > > foo(const T& p);
      > >
      > > in terms of use, efficiency, use of temporary objects and so on...
      > > ...[/color]
      >
      > No difference at all.[/color]

      (To the op): He means that "const" can appear before or after the type at
      your discretion (choice). It's optional in the language and has no bearing
      at runtime whatsoever. At compile time it's possible there could be a
      performance difference depending on the parsing techniques used but it would
      likely be so negligible that no programmer should ever worry about it (and
      in general there are standard algorithms for parsing declarators anyway).
      BTW, most generally use:

      const T &p

      but some like:

      T const &p

      typically because it can be read from right to left as in "p" is "&"
      (reference) to a "const" object of type "T". However, reading the first
      example becomes second nature very quickly so the latter argument is hollow
      IMO.


      Comment

      • Joshua Boelter

        #4
        Re: reference-to-const

        Mark Kerns wrote:[color=blue]
        > typically because it can be read from right to left as in "p" is "&"
        > (reference) to a "const" object of type "T". However, reading the first
        > example becomes second nature very quickly so the latter argument is hollow
        > IMO.[/color]

        Templates are one good reason to put your const to the right:

        typedef char* CHARPTR;

        const CHARPTR p;

        What is p?
        pointer to const char?
        const pointer to char?
        const pointer to const char?

        Or this:

        template < typename T >
        struct widget
        {
        const T p;
        };

        What is widget< char * >::p ?


        for the typedef

        const CHARPTR p;
        CHARPTR const p;
        char * const p;

        for the typename

        const T p;
        T const p;
        char * const p;

        In both cases p is a const pointer to char.

        Joshua Boelter
        These are my opinions not official opinions of Intel Corp.

        Comment

        • Andrey Tarasevich

          #5
          Re: reference-to-const

          Joshua Boelter wrote:[color=blue][color=green]
          >> typically because it can be read from right to left as in "p" is "&"
          >> (reference) to a "const" object of type "T". However, reading the first
          >> example becomes second nature very quickly so the latter argument is hollow
          >> IMO.[/color]
          >
          > Templates are one good reason to put your const to the right:
          >[/color]

          Sorry, but I don't see how your code samples illustrate that const
          should be put "to the right". It has always been a matter of personal
          preference. Templates (and/or typedefs) don't change anything here.

          --
          Best regards,
          Andrey Tarasevich

          Comment

          • Ron Natalie

            #6
            Re: reference-to-const


            "Joshua Boelter" <joshuaDOTboelt er@intel.com> wrote in message news:bnreii$siu $1@news01.intel .com...
            [color=blue]
            > Templates are one good reason to put your const to the right:[/color]

            Really? How so?
            [color=blue]
            >
            > typedef char* CHARPTR;
            >
            > const CHARPTR p;
            >
            > What is p?
            > pointer to const char?
            > const pointer to char?
            > const pointer to const char?[/color]

            To anybody who knows C++, it's obvious. Typedef and template
            are not just text subsitution. The rules for the declarations are
            consistant:

            const CHARPTR p;
            const T p;

            In either case p, is a const instance of whatever T or CHARPTR is.

            What's hard about it, and how does putting the const after the rest of
            the type name make any difference on the understanding?


            Comment

            • Mark Kerns

              #7
              Re: reference-to-const

              > Templates are one good reason to put your const to the right:

              Your examples aren't typical of most real-world code but the point is
              well-taken in spite of the objections of the others. That is, for an example
              such as:

              const CHARPTR p;
              CHARPTR const p;

              The rules certainly don't change but a less experienced developer may not be
              able to get his mind around this as quickly as an experienced developer (who
              won't be tripped up by this at all). This type of obfuscating code violates
              the KISS principle in general however and should generally be avoided IMO.
              When it is used though (I have seen it before) no programmer who knows what
              he's doing will misunderstand it so your point is noted though I consider it
              very weak :)


              Comment

              • Rolf Magnus

                #8
                Re: reference-to-const

                Joshua Boelter wrote:
                [color=blue]
                > Mark Kerns wrote:[color=green]
                >> typically because it can be read from right to left as in "p" is "&"
                >> (reference) to a "const" object of type "T". However, reading the
                >> first example becomes second nature very quickly so the latter
                >> argument is hollow IMO.[/color]
                >
                > Templates are one good reason to put your const to the right:
                >
                > typedef char* CHARPTR;
                >
                > const CHARPTR p;
                >
                > What is p?
                > pointer to const char?
                > const pointer to char?
                > const pointer to const char?[/color]

                If at all, I'd actually see this as one of the reasons to avoid hiding
                pointers behind typedefs.

                Comment

                • Micah Cowan

                  #9
                  Re: reference-to-const

                  Joshua Boelter <joshuaDOTboelt er@intel.com> writes:
                  [color=blue]
                  > Mark Kerns wrote:[color=green]
                  > > typically because it can be read from right to left as in "p" is "&"
                  > > (reference) to a "const" object of type "T". However, reading the first
                  > > example becomes second nature very quickly so the latter argument is hollow
                  > > IMO.[/color]
                  >
                  > Templates are one good reason to put your const to the right:
                  >
                  > typedef char* CHARPTR;
                  >
                  > const CHARPTR p;
                  >
                  > What is p?
                  > pointer to const char?
                  > const pointer to char?
                  > const pointer to const char?
                  >
                  > Or this:
                  >
                  > template < typename T >
                  > struct widget
                  > {
                  > const T p;
                  > };
                  >
                  > What is widget< char * >::p ?[/color]

                  const pointer to char. This is easy to realize once you realize
                  that the types denoted by typenames or type aliases (typedefs)
                  can't be messed with "on the inside", you can only modify them
                  "externally ".
                  [color=blue]
                  > for the typedef
                  >
                  > const CHARPTR p;
                  > CHARPTR const p;
                  > char * const p;
                  >
                  > for the typename
                  >
                  > const T p;
                  > T const p;
                  > char * const p;
                  >
                  > In both cases p is a const pointer to char.[/color]

                  Right. So what difference does it make whether the const is to
                  the right or to the left? It's stylistic: some people find one
                  way more readable, others another, and some find them both
                  equally legible.

                  --
                  Micah J. Cowan
                  micah@cowan.nam e

                  Comment

                  Working...