const function() !!??

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

    #16
    Re: const function() !!??


    "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
    news:10tj3pqaud 7boc0@news.supe rnews.com...[color=blue]
    > dandelion wrote:[color=green][color=darkred]
    > >>...
    > >> That's a good reason for returning a pointer-to-const-whatever, but I
    > >> think the OP was asking why you would want to return a const-whatever
    > >> (where "whatever" itself may or may not be a pointer type). As far as
    > >> I can tell, there's no reason to do so.[/color]
    > >
    > > const int foobar[] =
    > > {
    > > FOOBAR1, /* Some config data */
    > > FOOBAR2,
    > > FOOBAR3
    > > };
    > >
    > > const int get_foobar(int i)
    > > {
    > > assert(i<3);
    > > return foobar[i];
    > > }
    > > ...[/color]
    >
    > And how and when is this better than just
    >
    > int get_foobar(int i)
    >
    > with the same body?[/color]

    It's not much better anytime, anywhere. But hey... I was just looking for a
    way (any way) that would make returning a const int a bit more sensible.
    Best it could *possibly* do is to provide a hint to the programmer that
    modifying the result of get_foobar(int i) may yield undesired results.

    Otherwise the two are equivalent in all respects (as you know).



    Comment

    • Keith Thompson

      #17
      Re: const function() !!??

      "dandelion" <dandelion@mead ow.net> writes:[color=blue]
      > "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
      > news:10tj3pqaud 7boc0@news.supe rnews.com...[color=green]
      >> dandelion wrote:[color=darkred]
      >> >>...
      >> >> That's a good reason for returning a pointer-to-const-whatever, but I
      >> >> think the OP was asking why you would want to return a const-whatever
      >> >> (where "whatever" itself may or may not be a pointer type). As far as
      >> >> I can tell, there's no reason to do so.
      >> >
      >> > const int foobar[] =
      >> > {
      >> > FOOBAR1, /* Some config data */
      >> > FOOBAR2,
      >> > FOOBAR3
      >> > };
      >> >
      >> > const int get_foobar(int i)
      >> > {
      >> > assert(i<3);
      >> > return foobar[i];
      >> > }
      >> > ...[/color]
      >>
      >> And how and when is this better than just
      >>
      >> int get_foobar(int i)
      >>
      >> with the same body?[/color]
      >
      > It's not much better anytime, anywhere. But hey... I was just looking for a
      > way (any way) that would make returning a const int a bit more sensible.
      > Best it could *possibly* do is to provide a hint to the programmer that
      > modifying the result of get_foobar(int i) may yield undesired results.
      >
      > Otherwise the two are equivalent in all respects (as you know).[/color]

      How would the programmer modify the result of get_foobar()?

      (Incidentally, you'd also want an "assert(i>=0);" .)

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • lawrence.jones@ugs.com

        #18
        Re: const function() !!??

        Lawrence Kirby <lknews@netacti ve.co.uk> wrote:[color=blue]
        >
        > * - there are some issues arising from being able to return a structure or
        > union type but nothing very interesting in this respect.[/color]

        Well, they're interesting in that they are the only way that you can
        even attempt to modify the return value of a function, but you get
        undefined behavior if you do.

        -Larry Jones

        Some people just don't have inquisitive minds. -- Calvin

        Comment

        • Villy Kruse

          #19
          Re: const function() !!??

          On Wed, 05 Jan 2005 07:09:12 GMT,
          lawrence.jones@ ugs.com <lawrence.jones @ugs.com> wrote:

          [color=blue]
          > Lawrence Kirby <lknews@netacti ve.co.uk> wrote:[color=green]
          >>
          >> * - there are some issues arising from being able to return a structure or
          >> union type but nothing very interesting in this respect.[/color]
          >
          > Well, they're interesting in that they are the only way that you can
          > even attempt to modify the return value of a function, but you get
          > undefined behavior if you do.
          >[/color]

          How would you actualy do that? Can you take the address of such a
          returned structure and assign it to an appropriate pointer, and use that
          pointer to modify the value? If you assign the returned structure to
          another structure variable then the other scruture becomes a copy of the
          returned sctructer, doesn't it?

          Villy

          Comment

          • lawrence.jones@ugs.com

            #20
            Re: const function() !!??

            Villy Kruse <vek@station02. ohout.pharmapar tners.nl> wrote:[color=blue]
            >
            > How would you actualy do that? Can you take the address of such a
            > returned structure and assign it to an appropriate pointer, and use that
            > pointer to modify the value?[/color]

            No, the structure is not an lvalue so you can't take its address. The
            structure must contain an array -- in order to allow you to reference an
            element of such an array, the array is converted into a pointer to its
            first element *even when the array is not an lvalue*. So somthing like:

            f().a[0] = 6;

            violates no constraints and attempts to modify the function return value
            (which results in undefined behavior).

            -Larry Jones

            I always have to help Dad establish the proper context. -- Calvin

            Comment

            • Andrey Tarasevich

              #21
              Re: const function() !!??

              dandelion wrote:[color=blue][color=green][color=darkred]
              >> > ...
              >> > const int get_foobar(int i)
              >> > {
              >> > assert(i<3);
              >> > return foobar[i];
              >> > }
              >> > ...[/color]
              >>
              >> And how and when is this better than just
              >>
              >> int get_foobar(int i)
              >>
              >> with the same body?[/color]
              >
              > It's not much better anytime, anywhere. But hey... I was just looking for a
              > way (any way) that would make returning a const int a bit more sensible.
              > Best it could *possibly* do is to provide a hint to the programmer that
              > modifying the result of get_foobar(int i) may yield undesired results.
              > ...[/color]

              But there's no way to even attempt to modify the result of 'get_foobar'
              in C language! There's no need for any hint, for that reason.

              --
              Best regards,
              Andrey Tarasevich

              Comment

              • thesushant@rediffmail.com

                #22
                Re: const function() !!??

                this simply means that u don hve the concept of cost qualifier...

                Comment

                • dandelion

                  #23
                  Re: const function() !!??


                  "Keith Thompson" <kst-u@mib.org> wrote in message
                  news:lnacroj4u8 .fsf@nuthaus.mi b.org...

                  <snip>
                  [color=blue][color=green]
                  > > It's not much better anytime, anywhere. But hey... I was just looking[/color][/color]
                  for a[color=blue][color=green]
                  > > way (any way) that would make returning a const int a bit more sensible.
                  > > Best it could *possibly* do is to provide a hint to the programmer that
                  > > modifying the result of get_foobar(int i) may yield undesired results.
                  > >
                  > > Otherwise the two are equivalent in all respects (as you know).[/color]
                  >
                  > How would the programmer modify the result of get_foobar()?[/color]

                  Like you would any other integer. Assign it to ordinary 'int' and do
                  whatever it
                  is you want to do.

                  <example code>
                  #include <stdlib.h>
                  #include <stdio.h>

                  const int foo(void)
                  {
                  return 1;
                  }

                  int main(void)
                  {
                  int x;
                  x = foo() + 3;
                  x++;

                  x = 2*x + 1;

                  printf("x = %d\n", x);

                  return EXIT_SUCCESS;
                  }
                  </example code>
                  [color=blue]
                  > (Incidentally, you'd also want an "assert(i>=0);" .)[/color]

                  True, if this were production code.

                  I left it out since it's demonstration code, written to make a point which
                  wasn't about the usefullness of 'assert()'. I also omitted any #defines for
                  FOOBARx.


                  Comment

                  • dandelion

                    #24
                    Re: const function() !!??


                    <thesushant@red iffmail.com> wrote in message
                    news:1105075752 .706730.42280@f 14g2000cwb.goog legroups.com...[color=blue]
                    > this simply means that u don hve the concept of cost qualifier...[/color]

                    As has been pointed out by several people to several other people, it is
                    handy to include at least a
                    snippet of the post you are responding to in order to make it easyer for
                    other people to ascertain what the [BEEEP] you are talking about.


                    Comment

                    • dandelion

                      #25
                      Re: const function() !!??

                      "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
                      [color=blue]
                      > But there's no way to even attempt to modify the result of 'get_foobar'
                      > in C language![/color]

                      Ummm... Not sure what is meant here.
                      [color=blue]
                      > There's no need for any hint, for that reason.[/color]

                      Tastes differ. I did not say the code as presented is particularly usefull,
                      but still,
                      It's within the standard.


                      Comment

                      • Andrey Tarasevich

                        #26
                        Re: const function() !!??

                        dandelion wrote:[color=blue]
                        > ...[color=green]
                        >> But there's no way to even attempt to modify the result of 'get_foobar'
                        >> in C language![/color]
                        >
                        > Ummm... Not sure what is meant here.
                        > ...[/color]

                        Return values of functions in C are rvalues. It is not possible to
                        modify an rvalue. In C there no way to even attempt to modify an rvalue.
                        The notion of "modificati on" is not applicable to rvalues at all.
                        There's no need to protect rvalues from modifications by using 'const',
                        because the fact that they are rvalues already protects them better than
                        any 'const' will ever be able to. That's what I meant.

                        --
                        Best regards,
                        Andrey Tarasevich

                        Comment

                        • Keith Thompson

                          #27
                          Re: const function() !!??

                          "dandelion" <dandelion@mead ow.net> writes:[color=blue]
                          > "Keith Thompson" <kst-u@mib.org> wrote in message
                          > news:lnacroj4u8 .fsf@nuthaus.mi b.org...
                          >
                          > <snip>
                          >[color=green][color=darkred]
                          >> > It's not much better anytime, anywhere. But hey... I was just
                          >> > looking for a way (any way) that would make returning a const int
                          >> > a bit more sensible. Best it could *possibly* do is to provide a
                          >> > hint to the programmer that modifying the result of
                          >> > get_foobar(int i) may yield undesired results.
                          >> >
                          >> > Otherwise the two are equivalent in all respects (as you know).[/color]
                          >>
                          >> How would the programmer modify the result of get_foobar()?[/color]
                          >
                          > Like you would any other integer. Assign it to ordinary 'int' and do
                          > whatever it is you want to do.
                          >
                          > <example code>[/color]
                          [snip]

                          No, that's not modifying the result, it's modifying the value of a
                          *copy* of the result.

                          The function in question has been lost in snippage; it looked
                          something like this:

                          const int get_foobar(int i)
                          {
                          return blah;
                          }

                          Declaring the function to return a "const int" does not imply that the
                          caller shouldn't modify the value of a copy of the result. Indeed, it
                          would almost certainly be absurd to suggest such a thing. The copy is
                          the caller's own variable, no longer associated iwth the function in
                          any way, and the caller can do whatever it likes with it.

                          C provides no mechanism to even *attempt* to modify the actual result
                          of a function, any more than it allows you to modify a constant such
                          as 42. A function result is an rvalue.

                          An attempt to modify the function result might look like this:

                          get_foobar(2) = 10; /* illegal */

                          but of course that's not valid C, any more than

                          42 = 10; /* illegal */

                          is.

                          C allows you to declare that a function returns a const result, but
                          it's completely useless to do so, even as a hint to the programmer.

                          (Conceivably you could add a "const" to a function declaration to
                          force a pointer to that function to be incompatible with some other
                          function pointer, but that's ugly, and I'm not sure you can depend on
                          the compiler to enforce it.)

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                          We must do something. This is something. Therefore, we must do this.

                          Comment

                          • lawrence.jones@ugs.com

                            #28
                            Re: const function() !!??

                            Andrey Tarasevich <andreytarasevi ch@hotmail.com> wrote:[color=blue]
                            >
                            > Return values of functions in C are rvalues. It is not possible to
                            > modify an rvalue. In C there no way to even attempt to modify an rvalue.[/color]

                            There is in C99 as I explained earlier in this thread. It's horribly
                            obscure (the rvalue in question must be a struct containing an array)
                            and it (deservedly) results in undefined behavior if you do it, but it
                            is possible to attempt it.

                            -Larry Jones

                            We don't ATTEND parties, we just CRASH 'em. -- Calvin

                            Comment

                            Working...