Topic: question about returning void


Author: danielgutson@hotmail.com (danielgutson@hotmail.com)
Date: Thu, 29 May 2003 04:52:49 +0000 (UTC)
Raw View
Hi,
 Could somebody confirm please that this is illegal?

void f() { ... }

void g()
{
  return f();
}

I tried with 3 compilers and got different results (error and passed).
I also looked at the standard, but couldn   t arrive to a clear assertion.

Thanks!
  Daniel.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jdennett@acm.org (James Dennett)
Date: Thu, 29 May 2003 15:52:44 +0000 (UTC)
Raw View
danielgutson@hotmail.com wrote:
> Hi,
>  Could somebody confirm please that this is illegal?
>=20
> void f() { ... }
>=20
> void g()
> {
>   return f();
> }
>=20
> I tried with 3 compilers and got different results (error and passed).
> I also looked at the standard, but couldn=B4t arrive to a clear asserti=
on.

It's legal -- I believe the intention is to simplify
generic code, where forwarding functions often take
a similar form.

6.6.3/3 of the C++ standard covers this.

-- James.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: tslettebo@chello.no.nospam (=?iso-8859-1?Q?Terje_Sletteb=F8?=)
Date: Thu, 29 May 2003 15:53:22 +0000 (UTC)
Raw View
<danielgutson@hotmail.com> wrote in message news:23478c42.0305282002.7bf0=
1ecc@posting.google.com...
> Hi,
>  Could somebody confirm please that this is illegal?
>=20
> void f() { ... }
>=20
> void g()
> {
>   return f();
> }

Sorry, no, it's legal. ;)

It's intended to make it easier for generic code, without having to speci=
alise for void, so that you can do e.g. like this (to use the code above)=
:

template<class T>
T f() { return T(); }

template<class T>
T g()
{
  return f<T>();
}

int main()
{
  g<void>();
}

This means you can even do "return void();", as if you're creating a "voi=
d object". It's just the same as "return;", though.

> I tried with 3 compilers and got different results (error and passed).
> I also looked at the standard, but couldn=B4t arrive to a clear asserti=
on.

See [3.9.1/9]:

"The void type has an empty set of values. The void type is an incomplete=
 type that cannot be completed. It is used as the return type for functio=
ns that do not return a value. Any expression can be explicitly converted=
 to type cv void (5.4). An expression of type void shall be used only as =
an expression statement (6.2), as an operand of a comma expression (5.18)=
, as a second or third operand of ?: (5.16), as the operand of typeid, or=
 as the expression in a return statement (6.6.3) for a function with the =
return type void."

and [6.6.3/3]:

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


Regards,

Terje

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rmaddox@isicns.com (Randy Maddox)
Date: Thu, 29 May 2003 15:55:01 +0000 (UTC)
Raw View
danielgutson@hotmail.com (danielgutson@hotmail.com) wrote in message news:<23478c42.0305282002.7bf01ecc@posting.google.com>...
> Hi,
>  Could somebody confirm please that this is illegal?
>
> void f() { ... }
>
> void g()
> {
>   return f();
> }
>
> I tried with 3 compilers and got different results (error and passed).
> I also looked at the standard, but couldn   t arrive to a clear assertion.
>
> Thanks!
>   Daniel.
>

I could be wrong, and have been before, and I'm sure someone will
correct me if so, but it looks to me as if this should be perfectly
legal.  The sections of the standard upon which I base this conclusion
are:

3.9.1/9:  It (void) is used as the return type for functions that do
not return a value. ... An expression of type void shall be used only
as ... the expression in a return statement for a function with the
return type void.

5.2.2/3:  The type of the function call expression is the return type
of the statically chosen function .... This type shall be ..., or the
type void.

6.6.3/3:  A return statement with an expression of type "cv void" can
be used only in functions with a return type of cv void.

In your example you are using a function call expression with type
void in the return statement of a function with a return type of void.
 Based on the above referenced sections of the standard that appears
legal to me.

Randy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: marco@technoboredom.net (Marco Manfredini)
Date: Thu, 29 May 2003 15:55:46 +0000 (UTC)
Raw View
danielgutson@hotmail.com wrote:

> Hi,
>  Could somebody confirm please that this is illegal?
>=20
> void f() { ... }
>=20
> void g()
> {
>   return f();
> }
>=20
> I tried with 3 compilers and got different results (error and passed).
> I also looked at the standard, but couldn=B4t arrive to a clear
> assertion.

Returning void *is* allowed (It has been allowed to simplify work with
templates). See 6.6.3(3) in the standard.

M.

--=20
The text above is a result of a bug in my newsreader and I take no=20
responsibility for this text appearing in my post.=20

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: do-not-spam-ben.hutchings@businesswebsoftware.com (Ben Hutchings)
Date: Thu, 29 May 2003 15:55:56 +0000 (UTC)
Raw View
In article <23478c42.0305282002.7bf01ecc@posting.google.com>,
danielgutson@hotmail.com wrote:
> Hi,
>  Could somebody confirm please that this is illegal?
>=20
> void f() { ... }
>=20
> void g()
> {
>   return f();
> }
>=20
> I tried with 3 compilers and got different results (error and passed).
> I also looked at the standard, but couldn=B4t arrive to a clear asserti=
on.

It is clearly specified as legal in section 3.9.1 paragraph 9 and section
6.6.3 paragraph 3.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Thu, 29 May 2003 15:56:19 +0000 (UTC)
Raw View
In article <23478c42.0305282002.7bf01ecc@posting.google.com>,
"danielgutson@hotmail.com" <danielgutson@hotmail.com> writes
>Hi,
> Could somebody confirm please that this is illegal?

No, because it isn't. It is always allowed to return the 'return value'
of a function if it matches the return specification of the calling
function.
 i.e.

whatever foo(){...}

whatever bar(){
        return foo();
}

Is always valid. That change was made to include void partly motivated
by the need for such for templates to work as expected even with
functions returning void.


>
>void f() { ... }
>
>void g()
>{
>  return f();
>}
>
>I tried with 3 compilers and got different results (error and passed).
>I also looked at the standard, but couldn=B4t arrive to a clear assertio=
n.

--=20
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kristov@arcor.de ("Christoph Schulz")
Date: Thu, 29 May 2003 15:56:19 +0000 (UTC)
Raw View
<danielgutson@hotmail.com> wrote:

> Hi,
>  Could somebody confirm please that this is illegal?
>
> void f() { ... }
>
> void g()
> {
>   return f();
> }

It's legal. The standard says (6.6.3p3):

  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.

f() is an expression of type "cv void" and g() is a function
with a return type of cv void, so it's allowed.

Regards,
  Christoph


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: comeau@panix.com (Greg Comeau)
Date: Fri, 30 May 2003 23:27:40 +0000 (UTC)
Raw View
In article <23478c42.0305282002.7bf01ecc@posting.google.com>,
danielgutson@hotmail.com <danielgutson@hotmail.com> wrote:
> Could somebody confirm please that this is illegal?
>
>void f() { ... }
>
>void g()
>{
>  return f();
>}
>
>I tried with 3 compilers and got different results (error and passed).
>I also looked at the standard, but couldn   t arrive to a clear assertion.

It's fine accto Standard C++.   Some compilers may still not
accept it at all, but be sure you're running them in their
strictest and ansi'est modes, realizing it may complete with
something else you're trying to accomplish.
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
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?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]