return values for void functions

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

    return values for void functions

    The other day I did something like this:

    void foo
    {
    }

    void bar()
    {
    return foo();
    }

    when I meant to do this:

    void foo
    {
    }

    void bar()
    {
    foo();
    return;
    }

    I was suprised that it worked. Is this legal?

    Thanks.





  • Victor Bazarov

    #2
    Re: return values for void functions

    REH wrote:[color=blue]
    > The other day I did something like this:
    >
    > void foo
    > {
    > }
    >
    > void bar()
    > {
    > return foo();
    > }
    >
    > when I meant to do this:
    >
    > void foo
    > {
    > }
    >
    > void bar()
    > {
    > foo();
    > return;
    > }
    >
    > I was suprised that it worked. Is this legal?[/color]

    Yes.

    V

    Comment

    • enki

      #3
      Re: return values for void functions

      That is important because if you have a series if if statments. You
      can get out of the function when your condition is met. There is also
      void func(int &a, int &b, char &c){}

      Comment

      • Howard Gardner

        #4
        Re: return values for void functions

        REH wrote:[color=blue]
        > The other day I did something like this:
        >
        > void foo
        > {
        > }
        >
        > void bar()
        > {
        > return foo();
        > }
        >
        > when I meant to do this:
        >
        > void foo
        > {
        > }
        >
        > void bar()
        > {
        > foo();
        > return;
        > }
        >
        > I was suprised that it worked. Is this legal?
        >
        > Thanks.[/color]

        Yes it is, and the fact that it is lets you cover the case of a function
        returning void with code like:

        template < typename xResult >
        xResult foo(xResult (*fFunc)())
        {
        return (*fFunc)();
        }

        instead of making you write a special case that handls functions that
        return void, for example:

        void foo(void (*fFunc)())
        {
        *fFunc();
        }

        Having to write that second case to cover a return type of void would be
        a major pain.

        Comment

        • REH

          #5
          Re: return values for void functions


          "enki" <enki034@yahoo. com> wrote in message
          news:1107283710 .065305.64560@f 14g2000cwb.goog legroups.com...[color=blue]
          > That is important because if you have a series if if statments. You
          > can get out of the function when your condition is met. There is also
          > void func(int &a, int &b, char &c){}
          >[/color]

          I'm not sure what your point is.



          Comment

          • E. Robert Tisdale

            #6
            Re: return values for void functions

            REH wrote:
            [color=blue]
            > The other day I did something like this:[/color]
            [color=blue]
            > cat foo.cc[/color]
            void foo { }

            void bar(void) {
            return foo();
            }
            [color=blue]
            > g++ -Wall -ansi -pedantic -c foo.cc[/color]
            foo.cc:1: error: invalid function declaration
            foo.cc: In function `void bar()':
            foo.cc:4: error: `foo' undeclared (first use this function)
            foo.cc:4: error: (Each undeclared identifier \
            is reported only once for each function it appears in.)
            foo.cc:4: error: return-statement with a value, \
            in function returning 'void'

            You probably meant:
            [color=blue]
            > cat foo.cc[/color]
            void foo(void) { }

            void bar(void) {
            return foo();
            }
            [color=blue]
            > g++ -Wall -ansi -pedantic -c foo.cc
            > nm foo.o[/color]
            00000006 T _Z3barv
            00000000 T _Z3foov[color=blue]
            > c++filt _Z3barv[/color]
            bar()[color=blue]
            > c++filt _Z3foov[/color]
            foo()
            [color=blue]
            > when I meant to do this:
            >
            > void foo(void) { }
            >
            > void bar(void) {
            > foo();
            > return;
            > }
            >
            > I was suprised that it worked.[/color]

            I'm surprised too. My compiler doesn't like it.
            Are you "sure" that
            you actually compiled the code that you posted?
            [color=blue]
            > Is this legal?[/color]

            You probably shouldn't ask a question like this
            in the comp.lang.c++ newsgroup.
            Some of our subscribers know a lot about the standards
            but the real experts are the compiler developers.
            Submit your code to your compiler with options set
            for standard error checking and all warnings
            and it will tell you whether your code is legal or not.
            If you don't trust your compiler, submit your code to
            Greg Comeau's free online compiler at



            The compiler is a *much* more reliable judge of legal C++ code
            than "some guy" who subscribes to the comp.lang.c++ newsgroup.

            Comment

            • Howard

              #7
              Re: return values for void functions


              "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
              news:ctono9$pm9 $1@nntp1.jpl.na sa.gov...[color=blue]
              > REH wrote:
              >
              >
              > The compiler is a *much* more reliable judge of legal C++ code
              > than "some guy" who subscribes to the comp.lang.c++ newsgroup.[/color]

              I think that's a pretty bad assessment. How can someone know if their
              compiler is any good (compliant), if they don't themselves know what should
              be correct? This newsgroup has some folks who are pretty darn good at
              interpreting the standard doc's and presenting the OP with good responses.
              Who would you trust, your VC++ 6.0 compiler, or one of the regulars here?
              I'd listen to the regulars (and the FAQ), myself.

              -some guy


              Comment

              • Greg Comeau

                #8
                Re: return values for void functions

                In article <ctof9u$dst3@cu i1.lmms.lmco.co m>, REH <bogus@nowhere. net> wrote:[color=blue]
                >The other day I did something like this:
                >
                >void foo
                >{
                >}
                >
                >void bar()
                >{
                > return foo();
                >}
                >
                >when I meant to do this:
                >
                >void foo
                >{
                >}
                >
                >void bar()
                >{
                > foo();
                > return;
                >}
                >
                >I was suprised that it worked. Is this legal?[/color]

                Notwithstanding that your foo above is defined incorrectly,
                yes, it's legal. Section 6.6.3p3 of Standard C++ reads:
                "A return statement with an expression of type "cv void"
                can be used only in functions with a return type of cv void;
                the expression is evaluated just before the function returns
                to its caller."

                In case that's not clear 3.9.3p5 reads:
                "In this International Standard, the notation cv (or cv1, cv2, etc.),
                used in the description of types, represents an arbitrary set of
                cv-qualifiers, i.e., one of {const}, {volatile}, {const, volatile},
                or the empty set."

                As you mentioned, despite the legality, it may not necessarily
                be what you wanted, or it may be (this becomes more transparent
                with templates).
                --
                Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
                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

                • E. Robert Tisdale

                  #9
                  Re: return values for void functions

                  Some guy wrote:
                  [color=blue]
                  > E. Robert Tisdale wrote:
                  >[color=green]
                  >>The compiler is a *much* more reliable judge of legal C++ code
                  >>than "some guy" who subscribes to the comp.lang.c++ newsgroup.[/color]
                  >
                  > I think that's a pretty bad assessment.
                  > How can someone know if their compiler is any good (compliant)
                  > if they don't themselves know what should be correct?[/color]

                  First of all, there are *no* C++ compiler which comply fully
                  with the ANSI/ISO standards. The Comeau compilers come very close.
                  It is practically impossible for programmers to determine
                  for themselves whether any implementation complies or not.
                  [color=blue]
                  > This newsgroup has some folks
                  > who are pretty darn good at interpreting the standard doc's[/color]

                  And many more who are not.
                  How to you tell the difference.
                  [color=blue]
                  > and presenting the OP with good responses.
                  > Who would you trust, your VC++ 6.0 compiler, or one of the regulars here?[/color]

                  I *don't* trust the "regulars here".
                  The comp.lang.c++ newsgroup is *not* the most appropriate forum
                  to discuss the ANSI/ISO C++ standards or whether or not
                  any particular implementation complies.
                  The comp.std.c++ newsgroup is more appropriate.
                  The comp.std.c++ newsgroup attracts real experts
                  on the ANSI/ISO C++ standards so contributions
                  are more likely to be properly vetted.
                  [color=blue]
                  > I'd listen to the regulars (and the FAQ), myself.[/color]

                  That's probably bad advice. The first problem is that
                  we have no way to determine who the "regulars" are
                  much less whether or not they have any credentials,
                  training or experience in C++ compiler implementation.
                  We have no way to determine whether their interpretations
                  of the ANSI/ISO standards are correct or not.

                  Comment

                  • msalters

                    #10
                    Re: return values for void functions


                    E. Robert Tisdale wrote:[color=blue]
                    > Some guy wrote:
                    >[color=green]
                    > > E. Robert Tisdale wrote:
                    > >[color=darkred]
                    > >>The compiler is a *much* more reliable judge of legal C++ code
                    > >>than "some guy" who subscribes to the comp.lang.c++ newsgroup.[/color]
                    > >
                    > > I think that's a pretty bad assessment.
                    > > How can someone know if their compiler is any good (compliant)
                    > > if they don't themselves know what should be correct?[/color]
                    >
                    > First of all, there are *no* C++ compiler which comply fully
                    > with the ANSI/ISO standards. The Comeau compilers come very close.
                    > It is practically impossible for programmers to determine
                    > for themselves whether any implementation complies or not.[/color]

                    In all honesty, when you get to the point where Comeau is, you
                    discover that the standard is slightly fuzzy. It's not unusual
                    for a Comeau user to discover that some obscure case in the
                    standard cannot be implemented. Just have a look at the Core DR
                    lists.
                    [color=blue]
                    > I *don't* trust the "regulars here".
                    > The comp.lang.c++ newsgroup is *not* the most appropriate forum
                    > to discuss the ANSI/ISO C++ standards or whether or not
                    > any particular implementation complies.
                    > The comp.std.c++ newsgroup is more appropriate.
                    > The comp.std.c++ newsgroup attracts real experts
                    > on the ANSI/ISO C++ standards so contributions
                    > are more likely to be properly vetted.[/color]

                    Of course, the clc++m group attracts real experts as well. Reason
                    is simple, moderation keeps the traffic low enough. In fact, such
                    a trivial question would fit better here or in clc++m than in csc++
                    [color=blue][color=green]
                    > > I'd listen to the regulars (and the FAQ), myself.[/color]
                    >
                    > That's probably bad advice. The first problem is that
                    > we have no way to determine who the "regulars" are
                    > much less whether or not they have any credentials,
                    > training or experience in C++ compiler implementation.
                    > We have no way to determine whether their interpretations
                    > of the ANSI/ISO standards are correct or not.[/color]

                    You don't have to implement a compiler to know one is wrong.
                    In fact, once you gain some experience, you'll learn what's
                    easy and what's impossible without implementing one. And
                    for 99% of the questions here, any interpretation backed by
                    a quote is correct simply because there is no misinterpretati on
                    possible. The "expert" part is finding the right quote.
                    Regards,
                    Michiel Salters

                    Comment

                    • Richard Herring

                      #11
                      Re: return values for void functions

                      In message <ctp37e$2cr$1@n ntp1.jpl.nasa.g ov>, E. Robert Tisdale
                      <E.Robert.Tisda le@jpl.nasa.gov > writes[color=blue]
                      >Some guy wrote:
                      >[color=green]
                      >> E. Robert Tisdale wrote:
                      >>[color=darkred]
                      >>>The compiler is a *much* more reliable judge of legal C++ code
                      >>>than "some guy" who subscribes to the comp.lang.c++ newsgroup.[/color]
                      >> I think that's a pretty bad assessment.
                      >> How can someone know if their compiler is any good (compliant)
                      >> if they don't themselves know what should be correct?[/color]
                      >
                      >First of all, there are *no* C++ compiler which comply fully
                      >with the ANSI/ISO standards.[/color]

                      Which rules out *your* suggestion above that the compiler is an
                      appropriate tool to test compliance.
                      [color=blue]
                      >The Comeau compilers come very close.
                      >It is practically impossible for programmers to determine
                      >for themselves whether any implementation complies or not.[/color]

                      In many cases, not all, it's trivial. Give it some test code, see
                      whether it compiles and how the compiled code behaves.

                      But you've digressed. The original question was about whether a piece of
                      *code* complied with the standard, not some compiler. Other posters
                      answered the question, complete with citations of the relevant portion
                      of the standard. If you don't trust them, you can read the standard for
                      yourself.[color=blue]
                      >[color=green]
                      >> This newsgroup has some folks who are pretty darn good at
                      >>interpretin g the standard doc's[/color]
                      >
                      >And many more who are not.
                      >How to you tell the difference.[/color]

                      By reading their reasoned arguments? By reading the standard for
                      yourself? The question here was straightforward enough, and so directly
                      answered by the standard, that questions of "interpretation " just didn't
                      arise.
                      [color=blue][color=green]
                      >> and presenting the OP with good responses. Who would you trust, your
                      >>VC++ 6.0 compiler, or one of the regulars here?[/color]
                      >
                      >I *don't* trust the "regulars here".[/color]

                      Good advice, since you're one of them.
                      [color=blue]
                      >The comp.lang.c++ newsgroup is *not* the most appropriate forum
                      >to discuss the ANSI/ISO C++ standards or whether or not
                      >any particular implementation complies.
                      >The comp.std.c++ newsgroup is more appropriate.[/color]

                      comp.std.c++ is the appropriate place for discussing the standard and
                      proposed changes to it. It's not necessarily the best place for
                      discussing compliance of some random compiler, still less some random
                      snippet of code.
                      [color=blue]
                      >The comp.std.c++ newsgroup attracts real experts
                      >on the ANSI/ISO C++ standards so contributions
                      >are more likely to be properly vetted.[/color]

                      Contributions making assertions about the standard, certainly.
                      [color=blue][color=green]
                      >> I'd listen to the regulars (and the FAQ), myself.[/color]
                      >
                      >That's probably bad advice. The first problem is that
                      >we have no way to determine who the "regulars" are
                      >much less whether or not they have any credentials,
                      >training or experience in C++ compiler implementation.[/color]

                      And how is that any different for the "regulars" of comp.std.c++?

                      Some of the "regulars" here make testable claims. Some of them have
                      names and reputations outside this newsgroup. Some of them *are* the
                      "real experts" you think inhabit comp.std.c++.
                      [color=blue]
                      >We have no way to determine whether their interpretations
                      >of the ANSI/ISO standards are correct or not.[/color]

                      We can read the standard for ourselves and follow their reasoned
                      arguments, can't we?

                      --
                      Richard Herring

                      Comment

                      • Jerry Coffin

                        #12
                        Re: return values for void functions

                        msalters wrote:

                        [ ... ]
                        [color=blue]
                        > Of course, the clc++m group attracts real experts as well. Reason
                        > is simple, moderation keeps the traffic low enough. In fact, such
                        > a trivial question would fit better here or in clc++m than in csc++[/color]

                        clc++/clc++m have similar aims: discussion of the language as it stands
                        right now. csc++ has a completely different aim: to discuss the
                        standard for the language, how it should be changed, etc.

                        The difference here is not one of the level of expertise, but of
                        orientation. Most of the regulars in csc++ also participate on a
                        regular basis in clc++ and/or clc++m as well.
                        [color=blue][color=green]
                        > > That's probably bad advice. The first problem is that
                        > > we have no way to determine who the "regulars" are
                        > > much less whether or not they have any credentials,
                        > > training or experience in C++ compiler implementation.[/color][/color]

                        Google makes it easy to find regulars, but posting and dependability
                        are (unfortunately) more or less orthogonal.

                        Standard conformance is largely a question of facts (or lack thereof),
                        and is thus easier to evaluate on its own merits. The place you really
                        want to look for expertise is when somebody is giving advice about
                        programming technique. Here you're (largely) taking a guess at what's
                        likely to become useful in the future, so you're hoping that the
                        expert's experience will guide you into making decisions that turn out
                        well -- but knowing up front that no matter what you do, you're
                        basically playing the odds, with no facts about "right" or "wrong"
                        available until after the fact.

                        Even so, I, at least, believe that your judgement should be based more
                        on the qualities apparent in the arguments themselves than in the
                        reputation of the person who makes them. Just for one example, I can
                        remember the first few posts Andrei Alexandrescu made to comp.lang.c++:
                        he had virtually never posted anything, anywhere before, and I'd
                        certainly never heard of him before (this was well before he was
                        published). Dismissing him out of hand on that basis would have been a
                        _major_ mistake, at least IMO.
                        [color=blue][color=green]
                        > > We have no way to determine whether their interpretations
                        > > of the ANSI/ISO standards are correct or not.[/color]
                        >
                        > You don't have to implement a compiler to know one is wrong.
                        > In fact, once you gain some experience, you'll learn what's
                        > easy and what's impossible without implementing one. And
                        > for 99% of the questions here, any interpretation backed by
                        > a quote is correct simply because there is no misinterpretati on
                        > possible. The "expert" part is finding the right quote.[/color]

                        I'm not at all sure I'd agree with the 99% number, but there are
                        certainly quite a few questions for which the answers are sufficiently
                        well known that one quote from the correct part of the standrad
                        suffices as an answer.

                        A single quote will rarely suffice for an expert-level question though.
                        A difficult question might easily require bits and pieces from a half
                        dozen (often widely-separated) parts of the standard to resolve.

                        Worse, the standard is sufficiently large that when those half dozen
                        parts are combined, the result can be _most_ surprising to those who
                        think they know the standard the best (it certainly suprises me on a
                        regular basis).

                        --
                        Later,
                        Jerry.

                        The universe is a figment of its own imagination.

                        Comment

                        • REH

                          #13
                          Re: return values for void functions


                          "Howard" <alicebt@hotmai l.com> wrote in message
                          news:3tTLd.3238 $xR1.1022@bgtns c04-news.ops.worldn et.att.net...[color=blue]
                          >
                          > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
                          > news:ctono9$pm9 $1@nntp1.jpl.na sa.gov...[color=green]
                          > > REH wrote:
                          > >
                          > >
                          > > The compiler is a *much* more reliable judge of legal C++ code
                          > > than "some guy" who subscribes to the comp.lang.c++ newsgroup.[/color]
                          >
                          > I think that's a pretty bad assessment. How can someone know if their
                          > compiler is any good (compliant), if they don't themselves know what[/color]
                          should[color=blue]
                          > be correct? This newsgroup has some folks who are pretty darn good at
                          > interpreting the standard doc's and presenting the OP with good responses.
                          > Who would you trust, your VC++ 6.0 compiler, or one of the regulars here?
                          > I'd listen to the regulars (and the FAQ), myself.
                          >
                          > -some guy
                          >[/color]
                          That's quoted wrong. He wrote that crap, not me!

                          REH




                          Comment

                          • REH

                            #14
                            Re: return values for void functions


                            "Greg Comeau" <comeau@panix.c om> wrote in message
                            news:ctp4qn$9ek $1@panix1.panix .com...[color=blue]
                            > In article <ctof9u$dst3@cu i1.lmms.lmco.co m>, REH <bogus@nowhere. net>[/color]
                            wrote:[color=blue][color=green]
                            > >The other day I did something like this:
                            > >
                            > >void foo
                            > >{
                            > >}
                            > >
                            > >void bar()
                            > >{
                            > > return foo();
                            > >}
                            > >
                            > >when I meant to do this:
                            > >
                            > >void foo
                            > >{
                            > >}
                            > >
                            > >void bar()
                            > >{
                            > > foo();
                            > > return;
                            > >}
                            > >
                            > >I was suprised that it worked. Is this legal?[/color]
                            >
                            > Notwithstanding that your foo above is defined incorrectly,[/color]

                            Yes, sorry, I was just trying too quickly to sketch an example of what I
                            wrote.
                            [color=blue]
                            > As you mentioned, despite the legality, it may not necessarily
                            > be what you wanted, or it may be (this becomes more transparent
                            > with templates).[/color]
                            Other than clarity or style/preferences (or the surprise!), is there a
                            reason why it's not what I wanted? I assume, from what I've been told, is
                            it's the same (functionally) as what I did want.

                            Thanks.


                            Comment

                            • Howard

                              #15
                              Re: return values for void functions


                              "REH" <bogus@nowhere. net> wrote in message
                              news:ctr8je$egr 2@cui1.lmms.lmc o.com...[color=blue]
                              >
                              > "Howard" <alicebt@hotmai l.com> wrote in message
                              > news:3tTLd.3238 $xR1.1022@bgtns c04-news.ops.worldn et.att.net...[color=green]
                              >>
                              >> "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
                              >> news:ctono9$pm9 $1@nntp1.jpl.na sa.gov...[color=darkred]
                              >> > REH wrote:
                              >> >
                              >> >
                              >> > The compiler is a *much* more reliable judge of legal C++ code
                              >> > than "some guy" who subscribes to the comp.lang.c++ newsgroup.[/color][/color][/color]
                              ....[color=blue][color=green]
                              >>[/color]
                              > That's quoted wrong. He wrote that crap, not me!
                              >
                              > REH[/color]

                              Sorry, I forgot to take out the line with "REH wrote:" when I snipped all
                              the stuff at the top.

                              -H


                              Comment

                              Working...