Topic: return void();


Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/10/21
Raw View
In article <7u523n$g2u$1@engnews1.eng.sun.com>, Steve Clamage
<clamage@eng.sun.com> writes
>What's wrong with this:
>
>class X;
>template< class T > class C { T* p; };
>C<X> c;
>
>I don't see any problem with C<void>, either.

My mistake through not thinking carefully enough.  In addition the
specific dispensation re return void in certain circumstances makes it
useful even when not using only T* or T&



Francis Glassborow      Journal Editor, Association of C & C++ Users
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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/14
Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:

>In article <7trmsh$3dj$1@nnrp1.deja.com>, Andrei Alexandrescu
><andrewalex@hotmail.com> writes
>>Consider this:
>>
>>template <class T> T f() { return T(); }
>>
>>int main()
>>{
>>    f<void>();
>>}
>>
>>Is the code correct?

>I do not believe that you are allowed to pass an incomplete type to a
>template type parameter. void is such a type and one that cannot be
>completed.

What's wrong with this:

class X;
template< class T > class C { T* p; };
C<X> c;

I don't see any problem with C<void>, either.

--
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: zivca@netvision.net.il (Ziv Caspi)
Date: 1999/10/15
Raw View
On 13 Oct 1999 21:50:23 GMT, Alain Miniussi <alain@sophia.cnrs.fr>
wrote:

>
>Andrei Alexandrescu wrote:
>>
>> Consider this:
>>
>> template <class T> T f() { return T(); }
>>
>> int main()
>> {
>>     f<void>();
>> }
>>
>> Is the code correct?
>
>void an incomplete type (like a declared, but not yet defined class). So
>you are not allowed to create an object of that type.
>(depending on that you are trying to do, you can consider to specialize
>your template for void).

How do you reconcile that with 5.2.3 para 1 ?

---------------------------------------------
Ziv Caspi
zivca@netvision.net.il
---
[ 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: Matt Austern <austern@sgi.com>
Date: 1999/10/15
Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:

> In article <7trmsh$3dj$1@nnrp1.deja.com>, Andrei Alexandrescu
> <andrewalex@hotmail.com> writes
> >Consider this:
> >
> >template <class T> T f() { return T(); }
> >
> >int main()
> >{
> >    f<void>();
> >}
> >
> >Is the code correct?
>
> I do not believe that you are allowed to pass an incomplete type to a
> template type parameter. void is such a type and one that cannot be
> completed.

No, it is permitted to pass incomplete types to template type
parameters, and there are even some cases in the library where we do
that.  Consider, for example, std::allocator<void>.  (20.4.1)

The restriction you have in mind, I think, is that (17.4.3.6/2),
unless otherwise specified, users are not allowed to instantiate
standard library templates with incomplete types.
---
[ 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: Alain Miniussi <alain@sophia.cnrs.fr>
Date: 1999/10/15
Raw View
Ziv Caspi wrote:
>
> On 13 Oct 1999 21:50:23 GMT, Alain Miniussi <alain@sophia.cnrs.fr>
> wrote:
>
> >
> >Andrei Alexandrescu wrote:
> >>
> >> Consider this:
> >>
> >> template <class T> T f() { return T(); }
> >>
> >> int main()
> >> {
> >>     f<void>();
> >> }
> >>
> >> Is the code correct?
> >
> >void an incomplete type (like a declared, but not yet defined class). So
> >you are not allowed to create an object of that type.
> >(depending on that you are trying to do, you can consider to specialize
> >your template for void).
>
> How do you reconcile that with 5.2.3 para 1 ?

See my follow up to the post you cite.
(by the way, it's para 2 :-)

Alain


[ 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: "Daniel M. Pfeffer" <pfefferd@nospam.internet-zahav.net>
Date: 1999/10/13
Raw View
1. IIRC, 'void' is not a type. It may be used to indicate that a function
returns nothing, or it may be used in a pointer to indicate a pointer to an
object of unspecified type.

2. The test here is simple - can you instantiate an object of type 'void'?
The answer is 'no', therefore your code is illegal.

--
Daniel Pfeffer
--------------
Remove 'nospam' from my address in order to contact me directly


Andrei Alexandrescu <andrewalex@hotmail.com> wrote in message
news:7trmsh$3dj$1@nnrp1.deja.com...
> Consider this:
>
> template <class T> T f() { return T(); }
>
> int main()
> {
>     f<void>();
> }
>
> Is the code correct?





[ 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: 1999/10/13
Raw View

> 1. IIRC, 'void' is not a type.

Void is a type.  There are however, specific things that can be done with
it.  From the standard:

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 functions that do not
return a value. Any expression can be explicitly converted to type cv void.
An expression of type void shall be used only as an expression statement,
as an operand of a comma expression as a second or third operand of ?: , as the operand
of typeid, or as the expression in a return statement for a function with the return type void.

> 2. The test here is simple - can you instantiate an object of type 'void'?
> The answer is 'no', therefore your code is illegal.
>

See the last line quoted above.



[ 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: rmacombe@nevis.u.arizona.edu (Robert J Macomber)
Date: 1999/10/13
Raw View
In article <7trmsh$3dj$1@nnrp1.deja.com>,
Andrei Alexandrescu  <andrewalex@hotmail.com> wrote:
>Consider this:
>
>template <class T> T f() { return T(); }
>
>int main()
>{
>    f<void>();
>}
>
>Is the code correct?

Yes.  A return statement in a void function is allowed to take an
expression of type void (6.6.3/3) and void() is a valid expression
(5.2.3/2)
--
   Rob Macomber
    (rmacombe@u.arizona.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: Alain Miniussi <alain@sophia.cnrs.fr>
Date: 1999/10/13
Raw View
Andrei Alexandrescu wrote:
>
> Consider this:
>
> template <class T> T f() { return T(); }
>
> int main()
> {
>     f<void>();
> }
>
> Is the code correct?

void an incomplete type (like a declared, but not yet defined class). So
you are not allowed to create an object of that type.
(depending on that you are trying to do, you can consider to specialize
your template for void).

Alain


[ 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: Barry Margolin <barmar@bbnplanet.com>
Date: 1999/10/13
Raw View
In article <3804C2D2.7B35EBE9@sensor.com>, Ron Natalie  <ron@sensor.com> wrote:
>
>
>> 1. IIRC, 'void' is not a type.
>
>Void is a type.  There are however, specific things that can be done with
>it.  From the standard:
>
>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 functions that do not
>return a value. Any expression can be explicitly converted to type cv void.
>An expression of type void shall be used only as an expression statement,
>as an operand of a comma expression as a second or third operand of ?: ,
>as the operand
>of typeid, or as the expression in a return statement for a function with
>the return type void.
>
>> 2. The test here is simple - can you instantiate an object of type 'void'?
>> The answer is 'no', therefore your code is illegal.
>>
>
>See the last line quoted above.

The reason there are so many incorrect answers to this question is probably
because that last line was a relatively recent addition to the language.
It was put in specifically to support templates like the one in the
original post.

--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/10/14
Raw View
In article <7trmsh$3dj$1@nnrp1.deja.com>, Andrei Alexandrescu
<andrewalex@hotmail.com> writes
>Consider this:
>
>template <class T> T f() { return T(); }
>
>int main()
>{
>    f<void>();
>}
>
>Is the code correct?

I do not believe that you are allowed to pass an incomplete type to a
template type parameter. void is such a type and one that cannot be
completed.


Francis Glassborow      Journal Editor, Association of C & C++ Users
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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Daniel Hartmeier <daniel@reichardt.ch>
Date: 1999/10/14
Raw View
On 11 Oct 99 05:09:26 GMT, Andrei Alexandrescu <andrewalex@hotmail.com>
wrote:

> Consider this:
>
> template <class T> T f() { return T(); }
>
> int main()
> {
>     f<void>();
> }
>
> Is the code correct?

IMHO no, because the following is already invalid:

  void f()
    {
      return void(); // function cannot return a value
    }

I searched the standard but couldn't find out what 'void()' could
possibly mean. Obviously it's a value of some kind and 'return void();'
is not the same as 'return;'.

It seems that 'void()' can be implicitly casted to int:

  int f()
    {
      return void(); // compiles without errors
    }

So, what IS '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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ron Natalie <ron@sensor.com>
Date: 1999/10/14
Raw View
Andrei Alexandrescu wrote:
>
> Consider this:
>
> template <class T> T f() { return T(); }
>
> int main()
> {
>     f<void>();
> }
>
> Is the code correct?
>

Sure.  The standard specifically allows void returning functions to
use void expressions in their return statement for just this sort
of reason.

Despite that it is specically allowed by the standard, I find that
not returning an explicit value from main is ludicrous.
---
[ 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: Alain Miniussi <alain@sophia.cnrs.fr>
Date: 1999/10/14
Raw View
Francis Glassborow wrote:
>
> In article <7trmsh$3dj$1@nnrp1.deja.com>, Andrei Alexandrescu
> <andrewalex@hotmail.com> writes
> >Consider this:
> >
> >template <class T> T f() { return T(); }
> >
> >int main()
> >{
> >    f<void>();
> >}
> >
> >Is the code correct?
>
> I do not believe that you are allowed to pass an incomplete type to a
> template type parameter.

The main restriction I have found concern local types, types with no
linkage and unnamed types (14.3.1/2).

I thought that the use of that type produced an incorrect construct
(14.3/6)
but I was wrong since void() is legal (somewhere in 5).

Alain
---
[ 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: 1999/10/14
Raw View
Francis Glassborow wrote:
>
> I do not believe that you are allowed to pass an incomplete type to a
> template type parameter.

Why not?  Do it frequently.

The standard restricts the invalid types to:

- A local type
- A type with no linkage
- an unnamed type
- or compounds of the above.
---
[ 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: Alain Miniussi <alain@sophia.cnrs.fr>
Date: 1999/10/14
Raw View
Alain Miniussi wrote:
> void an incomplete type (like a declared, but not yet defined class). So
> you are not allowed to create an object of that type.
> (depending on that you are trying to do, you can consider to specialize
> your template for void).

Sorry, I was wrong.
I based the above on:
 "Incompletely-defined object types and the void types are incomplete
types.
Objects shall not be defined to have an incomplete type."
But it's not a definition.

Alain


[ 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: Andrei Alexandrescu <andrewalex@hotmail.com>
Date: 1999/10/11
Raw View
Consider this:

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

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

Is the code correct?


Andrei


Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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              ]