Topic: return void


Author: scottm@toast.net (Scott)
Date: Wed, 13 Jun 2001 01:13:04 GMT
Raw View
In comp.std.c++, "Balog Pal" <pasa@lib.hu> wrote:


>whatever D::foo(ARG arg) { return tParent::foo( myargconverson(arg) ); }

>That is pretty cool and intuitive, but if 'whatever' happend to be void,
>another syntax is needed...

>Would that be dangerous if allowed?

I asked for this years ago in comp.std.c. I can think of no reason why it
was dangerous then, and I can't see a problem now. I've written automated
code generators that had to crock around the special status of void.

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Balog Pal" <pasa@lib.hu>
Date: Sun, 10 Jun 2001 15:49:13 GMT
Raw View
In a class hierarchy it's pretty common to see something like this in overridden functions:

whatever D::foo(ARG arg)
{
  // add specific stuff
  return tParent::foo(arg);
}

or

whatever D::foo(ARG arg)
{
  return tParent::foo( myargconverson(arg) );
}

That is pretty cool and intuitive, but if 'whatever' happend to be void, another syntax is needed.

I'd find it convenient if I could use the same syntax with void too, which the compiler would treat like for other types: evaluating whatever is afret return, then do a return. Currently I get error messages, like 'void functionreturning value', not exactly true void being not a value, or 'expression is void', quite true, but that's all needed. ;-)

Would that be dangerous if allowed?

Paul



---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Sun, 10 Jun 2001 16:19:26 GMT
Raw View
In article <3b237f79@andromeda.datanet.hu>, Balog Pal <pasa@lib.hu>
writes
>That is pretty cool and intuitive, but if 'whatever' happend to be void, another
>syntax is needed.

And so clearly was this silly, that the standard actually specifies that

void foo();

void bar(){
  return foo();
}

is correct.

>
>I'd find it convenient if I could use the same syntax with void too, which the
>compiler would treat like for other types: evaluating whatever is afret return,
>then do a return. Currently I get error messages, like 'void functionreturning
>value', not exactly true void being not a value, or 'expression is void', quite
>true, but that's all needed. ;-)
>
>Would that be dangerous if allowed?

It is allowed, just use an up-to-date compiler.


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.research.att.com/~austern/csc/faq.html                ]





Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/10/08
Raw View

Is something like this possible?  I've seen in done in one of the
examples in Stroustrup III.  It's very convenient.

void f() { /*stuff*/ }
void g() { /*stuff*/  return f();  /*stuff*/ }

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Xing Li <xing@math.msu.edu>
Date: 1998/10/08
Raw View
Siemel Naran wrote:
>
> Is something like this possible?  I've seen in done in one of the
> examples in Stroustrup III.  It's very convenient.
>
> void f() { /*stuff*/ }
> void g() { /*stuff*/  return f();  /*stuff*/ }
Compiler will simply consider that g() is trying to return something.
Thus it gives either a warning or a error message.

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: gurnec@my-dejanews.com
Date: 1998/10/08
Raw View
In article <361C3E73.674179EB@math.msu.edu>,
  xing@math.msu.edu wrote:
> Siemel Naran wrote:
> >
> > Is something like this possible?  I've seen in done in one of the
> > examples in Stroustrup III.  It's very convenient.
> >
> > void f() { /*stuff*/ }
> > void g() { /*stuff*/  return f();  /*stuff*/ }
> Compiler will simply consider that g() is trying to return something.
> Thus it gives either a warning or a error message.

Actually, the IS changed this rule from CD2.  Quoting 6.6.3 (The return
statement):

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.

One reason I know of why this was changed was to allow something like:

template <class UnaryFunction>
typename UnaryFunction::result_type
foo_then_function(UnaryFunction f, typename UnaryFunction::argument_type& x) {
  foo();
  return f(x);
}

without specializing for the case where UnaryFunction::result_type is void.

-Chris Gurnee

--

mangled email: gurnec_at_mediaone_dot_net

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1998/10/08
Raw View
Siemel Naran wrote:
>
> Is something like this possible?  I've seen in done in one of the
> examples in Stroustrup III.  It's very convenient.
>
> void f() { /*stuff*/ }
> void g() { /*stuff*/  return f();  /*stuff*/ }

Oddly enough, this is one of the few places things of type
void are allowed to be used:

- in expression statements:
 f();

- in the second or third argument to a ternary op
 bool_var ? f() : g()

- in the first argument of , op
 f(),23

- in a return from a fucnction defined as returning void
 your example...

Note that this is a feature that is WIDELY not supported.
As a matter of fact I can't find a single C++ compiler
here that eats 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/10/08
Raw View
sbnaran@fermi.ceg.uiuc.edu (Siemel Naran) writes:

>Is something like this possible?  I've seen in done in one of the
>examples in Stroustrup III.  It's very convenient.

>void f() { /*stuff*/ }
>void g() { /*stuff*/  return f();  /*stuff*/ }

It's a recent addition to the standard. It was formerly
forbidden. The reason for the change was not to write
code like the above, but to make it easier to write templates.
Without the relaxed rule, you would have to write a special
version of templates for instantiation on type void.

--
Steve Clamage, stephen.clamage@sun.com


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1998/10/08
Raw View
Xing Li wrote:

> Siemel Naran wrote:

> > Is something like this possible?  I've seen in done in one of the
> > examples in Stroustrup III.  It's very convenient.

> > void f() { /*stuff*/ }
> > void g() { /*stuff*/  return f();  /*stuff*/ }
> Compiler will simply consider that g() is trying to return something.
> Thus it gives either a warning or a error message.


Compiler is simply wrong.  The standard allows this.



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jim.hyslop@leitch.com
Date: 1998/10/08
Raw View
In article <slrn71nmc3.27e.sbnaran@fermi.ceg.uiuc.edu>,
  sbnaran@KILL.uiuc.edu wrote:
>
>
> Is something like this possible?  I've seen in done in one of the
> examples in Stroustrup III.  It's very convenient.
>
> void f() { /*stuff*/ }
> void g() { /*stuff*/  return f();  /*stuff*/ }

First off, if Stroustrup uses it, that's a pretty strong indicatation that
it's allowable syntax. However, even Stroustrup is human, and can make
mistakes (did I actually say that :-O )

Anyway, MSVC5 gives an error, complaining "g: 'void' function returning a
value".  But we all know that one compiler's opinion does not necessarily
indicate the truth, so let's see what the Standard says...

[pause while I dig through the Standard] Section 3.9.1 (Fundamental Types)
states "The void type has an empty set of values.... Any expression can be
explicitly converted to type cv void (5.2).... An expression of type void
shall be used only as ... the expression in a return statement (6.6.3) for a
function with the return type void."

OK, that suggests to me that "return void;" would be valid syntax.  Let's see
what MSVC says... nope, "error: type 'void' unexpected."  How about "return
(void) f();"... nope, back to the original error.

Let's look at 6.6.3 to see if it gives any clues.  Ah, here we go - clause 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".  Well, as I understand it, "f()"
is an expression of type 'cv void', so "return f();" should be valid syntax.

OK, now what do you real experts say to this?  Is my analysis correct?

Jim

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/02/06
Raw View
In comp.std.c++ Shai Spharim <shai@kswaves.com> wrote:
: return void

: With the introduction of templates to C++ the need arises to allow
: a void returning function to "return" a void or "return"
: the value returned from another void returning function.

: For example:

: Void A()
: {
:    //... blabla
:    return void; // error but why should it be?
: }

[ template example snipped ]

: Since a function returning void can only have an empty return
: statement.

: Has this idea been already considered?
: Can void act as a type at least in templates arguments?

Yes. It was discussed and it's now allowed, exactly for the needs
you describe.

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: shai@kswaves.com (Shai Spharim)
Date: 1998/02/04
Raw View
return void

With the introduction of templates to C++ the need arises to allow
a void returning function to "return" a void or "return"
the value returned from another void returning function.

For example:

Void A()
{
   //... blabla
   return void; // error but why should it be?
}

void B()
{
   // ... whatever
   return A(); // error again, but actually no harm done!
}

I found that limitation while writing the following code which is
a function-object template  that binds a class object to it's
member function:

template
<class ObjectType, class ReturnType, class ParamType>
class ObjectToFuncBinder
{
public:
typedef
ReturnType (ObjectType:: * FuncPtr) (const ParamType&);

typedef ObjectType * const ObjectPtr;

ObjectToFuncBinder(ObjectPtr in_pT, FuncPtr Func)
   : pO(in_pT) {}

ReturnType operator() (const ParamType& param)
{
   return (pO->*pF)(param);
}

private:
ObjectPtr   pO;
FuncPtr  pF;
};

template class ObjectToFuncBinder can be used in conjunction with
template functions like for_each, and works great,
unless ReturnType is void!

One cannot instantiate ObjectToFuncBinder with ReturnType
of type void.
The compiler will error the line:

return (pO->*pF)(param);

Since a function returning void can only have an empty return
statement.

Has this idea been already considered?
Can void act as a type at least in templates arguments?

Is there a way to overcome this limitation using the current
language?

--
Shai Spharim
shai@NoSpam.kswaves.com (remove the NoSpam.)
http://www.waves.com
___________________________________________
Computers are useless. They can only give you answers.
- Pablo Picasso.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]