const function() !!??

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

    #1

    const function() !!??

    Do we have something called a const function ? I thought applying the
    'const' specifier to a function is meaningless... but when i tried
    defining a const function it worked !!
    like...
    const int func(int a)
    {
    int b;
    .....
    return b;
    }

    ...The above code gave no compilation errors ! :->
    How should i interpret it ?
    (i used MS- vc++ compiler)

    Sundar
  • dandelion

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


    "Sundar" <sundar_sacet@y ahoo.com> wrote in message
    news:9e487261.0 412300544.4c4b0 c96@posting.goo gle.com...

    <snip>[color=blue]
    > const int func(int a)
    > {
    > int b;
    > .....
    > return b;
    > }
    >
    > ...The above code gave no compilation errors ! :->[/color]

    And it shouldn't.
    [color=blue]
    > How should i interpret it ?[/color]

    A function returning a const int, that is an non-modifyable integer.


    Comment

    • Emmanuel Delahaye

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

      Sundar wrote on 30/12/04 :[color=blue]
      > Do we have something called a const function ? I thought applying the[/color]

      No.
      [color=blue]
      > 'const' specifier to a function is meaningless...[/color]
      [color=blue]
      > defining a const function it worked !!
      > like...
      > const int func(int a)
      > {
      > int b;
      > .....
      > return b;
      > }[/color]

      That 'const' is useless.

      This one is useful:

      char const *get_ver (void)
      {
      return "1.2";
      }

      --
      Emmanuel
      The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
      The C-library: http://www.dinkumware.com/refxc.html

      "Clearly your code does not meet the original spec."
      "You are sentenced to 30 lashes with a wet noodle."
      -- Jerry Coffin in a.l.c.c++

      Comment

      • Andrey Tarasevich

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

        Sundar wrote:
        [color=blue]
        > Do we have something called a const function ? I thought applying the
        > 'const' specifier to a function is meaningless...[/color]

        You can't apply the 'const' specifier to a function in C. That would be
        a syntax error.
        [color=blue]
        > but when i tried
        > defining a const function it worked !!
        > like...
        > const int func(int a)
        > {
        > int b;
        > .....
        > return b;
        > }[/color]

        This is not a 'const' function. In accordance with C syntax, this
        'const' qualifier is applied to the return type of the function. I.e.
        this function return type is 'const int'.

        Syntactically, a naive attempt to apply a 'const' qualifier to a
        function would look as follows

        int (const func)(int a)

        but this simply won't compile.

        --
        Best regards,
        Andrey Tarasevich

        Comment

        • Keith Thompson

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

          "dandelion" <dandelion@mead ow.net> writes:[color=blue]
          > "Sundar" <sundar_sacet@y ahoo.com> wrote in message
          > news:9e487261.0 412300544.4c4b0 c96@posting.goo gle.com...
          >
          > <snip>[color=green]
          >> const int func(int a)
          >> {
          >> int b;
          >> .....
          >> return b;
          >> }
          >>
          >> ...The above code gave no compilation errors ! :->[/color]
          >
          > And it shouldn't.
          >[color=green]
          >> How should i interpret it ?[/color]
          >
          > A function returning a const int, that is an non-modifyable integer.[/color]

          But you can't modify the result of a function anyway.

          Is there any context in which there's a difference between
          const int func(int a) { /* blah blah */ }
          and
          int func(int a) { /* blah blah */ }
          ?

          As far as I can tell there isn't, and gcc warns:

          tmp.c:3: warning: type qualifiers ignored on function return type

          I suppose pointers to the two function types would be incompatible,
          but I can't think of any other difference.

          Probably it was just easier to allow it in the language definition,
          even though it's meaningless, than to disallow 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

          • __MMS__

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

            Thanks !
            I understand 'const' function is meaningless, but please tell me why to
            use a 'const' as return type ?!

            Comment

            • Raymond Martineau

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

              On 2 Jan 2005 00:45:30 -0800, "__MMS__" <sundar_sacet@y ahoo.com> wrote:
              [color=blue]
              >Thanks !
              >I understand 'const' function is meaningless, but please tell me why to
              >use a 'const' as return type ?![/color]

              Because the return value of the function would result in undefined
              behaviour if it were changed (e.g. returning a string literal).

              Comment

              • Stephen Sprunk

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

                "__MMS__" <sundar_sacet@y ahoo.com> wrote in message
                news:1104655530 .274804.119070@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                > Thanks !
                > I understand 'const' function is meaningless, but please tell me why to
                > use a 'const' as return type ?![/color]

                You may want to return a const pointer because you don't want the pointed-to
                object to be changed.

                S

                --
                Stephen Sprunk "Stupid people surround themselves with smart
                CCIE #3723 people. Smart people surround themselves with
                K5SSS smart people who disagree with them." --Aaron Sorkin

                Comment

                • Keith Thompson

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

                  "Stephen Sprunk" <stephen@sprunk .org> writes:[color=blue]
                  > "__MMS__" <sundar_sacet@y ahoo.com> wrote in message
                  > news:1104655530 .274804.119070@ z14g2000cwz.goo glegroups.com.. .[color=green]
                  >> Thanks !
                  >> I understand 'const' function is meaningless, but please tell me why to
                  >> use a 'const' as return type ?![/color]
                  >
                  > You may want to return a const pointer because you don't want the pointed-to
                  > object to be changed.[/color]

                  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.

                  --
                  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

                  • Andrey Tarasevich

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

                    __MMS__ wrote:[color=blue]
                    > ...
                    > I understand 'const' function is meaningless, but please tell me why to
                    > use a 'const' as return type ?!
                    > ...[/color]

                    There's absolutely no point in doing it in C. Values returned by
                    functions are rvalues. Const-qualification has no effect on rvalues in C
                    (in C++ things are different, but that's offtopic here).

                    --
                    Best regards,
                    Andrey Tarasevich

                    Comment

                    • dandelion

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


                      "Keith Thompson" <kst-u@mib.org> wrote in message
                      news:lnllbbmrkj .fsf@nuthaus.mi b.org...[color=blue]
                      > "Stephen Sprunk" <stephen@sprunk .org> writes:[color=green]
                      > > "__MMS__" <sundar_sacet@y ahoo.com> wrote in message
                      > > news:1104655530 .274804.119070@ z14g2000cwz.goo glegroups.com.. .[color=darkred]
                      > >> Thanks !
                      > >> I understand 'const' function is meaningless, but please tell me why to
                      > >> use a 'const' as return type ?![/color]
                      > >
                      > > You may want to return a const pointer because you don't want the[/color][/color]
                      pointed-to[color=blue][color=green]
                      > > object to be changed.[/color]
                      >
                      > 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];
                      }



                      Comment

                      • Sundar

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

                        Agree with you Keith :-)

                        Comment

                        • Andrey Tarasevich

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

                          dandelion wrote:[color=blue][color=green]
                          >>...
                          >> 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?

                          --
                          Best regards,
                          Andrey Tarasevich

                          Comment

                          • Keith Thompson

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

                            "Sundar" <sundar_sacet@y ahoo.com> writes:[color=blue]
                            > Agree with you Keith :-)[/color]

                            Thanks.

                            It's a good idea to provide some context, so readers can know what
                            you're referring to without going back to the parent article (which
                            may be difficult in some newsreaders). Since you didn't provide any
                            context, I'll just assume you agree with everything I say -- which I
                            must say is a remarkably enlightened attitude.

                            In case anyone is interested, the context was:

                            ] 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.

                            --
                            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 Kirby

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

                              On Sun, 02 Jan 2005 10:31:56 -0500, Raymond Martineau wrote:
                              [color=blue]
                              > On 2 Jan 2005 00:45:30 -0800, "__MMS__" <sundar_sacet@y ahoo.com> wrote:
                              >[color=green]
                              >>Thanks !
                              >>I understand 'const' function is meaningless, but please tell me why to
                              >>use a 'const' as return type ?![/color]
                              >
                              > Because the return value of the function would result in undefined
                              > behaviour if it were changed (e.g. returning a string literal).[/color]

                              It is simply not possible in C to change a value such as the return value
                              of a function (*). All you can do is operate on it to generate a new value.

                              In the case of a string literal you a pointer to const char to do what you
                              are saying, but the pointer itself isn't const.

                              Syntactically you can qualify the return type of a function but it serves
                              no purpose. A more evil thing is to define a const function which cna be
                              done using typedef e.g.

                              typedef void func(void);
                              typedef const func constfunc;

                              However the standard says that this has undefined behaviour.

                              * - there are some issues arising from being able to return a structure or
                              union type but nothing very interesting in this respect.

                              Lawrence

                              Comment

                              Working...