Topic: static_cast to void
Author: m-morita@trc.rwcp.or.jp (Masao Morita)
Date: 1999/09/15 Raw View
in static_cast to void.
> If I'm not mistaken, the quote from the standard said that the 'void' has
> no effect on when the object is destroyed.
[snip]
> ... Is my understanding correct?
You are right. I missed that quote.
Casting to void seems to help for only readability.
M. Morita
[ 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: Masao Morita <m-morita@pdss7.trc.rwcp.or.jp>
Date: 1999/09/13 Raw View
> >First one is a passive reason. It's for informing to a compliler discarding
> >the expression(the return value of a fnction call). As a result of this,
> >it makes avoiding accidental overlooking and suppresses the warning message
> >by a complier.
>
> Though it seems rare to me that one will ever want to use this feature.
> >Second one is a more active reason. It's for calling a destructor of a
> >temporary object immediately. When the calling sequence is very important,
> >it is useful. (I got it now!)
>
> Are you sure of this one?
Please consider.
struct Environment {
...
~Environment() { unlock_all_resources_with_check(); }
};
Environment setup();
void main() {
...
{
Environment a;
...
}
...
static_cast<void>(setup()),use_resource(); // add-hoc proc
}
Of cource, I know that the above is not good style, the other better
solution can be took place. But, sometimes it is convinent in some
situation.
Afterall, "static_cast to void" has some practical reasons, though
it has no favarable practical reason.
> > static_cast<const void>(SomeExpression);
> > static_cast<void>(SomeExpression);
> I think these are equivalent.
And cv-qualifer is meaningless.
Is my guess right?
M. Morita
---
[ 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: James.Kanze@dresdner-bank.com
Date: 1999/09/13 Raw View
In article <MPG.1244c6ff5b182d6e9896a9@news.mindspring.com>,
jcoffin@taeus.com (Jerry Coffin) wrote:
>
> In article <WwJQQZA6Jj23EwF3@robinton.demon.co.uk>,
> francis@robinton.demon.co.uk says...
>
> [ ... ]
> > Do such compilers exist in the real world? Because if so they must
> > be exceptionally noisy. Strictly speaking they should warn me for:
> > cout << 1;
> > as I will have ignore the return value of:
> > operator << (cout, 1);
>
> ...not to mention for such things as:
>
> x = 1;
> since this yields (not returns, since there's no function call) a
> value of 1...
> In all honesty, I've never seen (or heard of) a compiler with such
> stupid warnings, but I've seen at least one version of lint that did.
This is the second mention of lint I've seen here. The lint I remember
looked at the entire program (not just a module), and complained if the
return value of a function was used in some places, and not in others.
This was before there was a void type, and the warning was useful --
most of the time, it meant that you were trying to use a return value
from a function which didn't have one (but was implicitly int anyway).
And it didn't warn for cases like x = 1.
--
James Kanze mailto: James.Kanze@dresdner-bank.com
Conseils en informatique orient e objet/
Beratung in objekt orientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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: wmm@fastdial.net
Date: 1999/09/13 Raw View
In article <t0t906dizlg.fsf@pdss7.trc.rwcp.or.jp>,
Masao Morita <m-morita@pdss7.trc.rwcp.or.jp> wrote:
> Thank you for answering my question.
>
> > > static_cast<void>(Any_Expression);
> > >
> > >This expression looks like meaningless.
> > >Probably, the content of Any_expression would make sence in any case.
> > >what sort of case?
>
> > Generally casting to void is used to indicate that the (non-void) return
> > value of a function call is being intentionally ignored (rather than
> > accidentally overlooked). There are some coding standards that require
> > this to be done -- that the return value of a non-void function must either
> > be used or explicitly discarded by casting it to void.
>
> [snip]
> >
> > struct S { ~S(); };
> > S f();
> > int g();
> > void h() {
> > int i = (f(), g()); // #1
> > int j = ((void) f(), g()); // #2
> > }
> >
> > In both #1 and #2, the temporary used for the return value of the call to
> > f() is destroyed only after the call to g(); the cast to void in #2 makes
> > clear that the return value of f() is being intentionally ignored, but it
> > has no impact on when the temporary will be destroyed.
> I am appreciated the comment.
> I'm a little bit clear about "static_cast to void".
>
> There are two practical reasons.
>
> First one is a passive reason. It's for informing to a compliler discarding
> the expression(the return value of a fnction call). As a result of this,
> it makes avoiding accidental overlooking and suppresses the warning message
> by a complier.
Actually it is more for the human reader (for example, during a code
review). If you write "f()", a reader might wonder if you thought
there was no return value; if you write "(void) f()", the reader can
be sure that you were aware that a value was returned but are
intentionally ignoring it. It's similar to putting a comment like
"/* FALLTHROUGH */" at the end of a case in a switch statement when
you intentionally omit the "break" -- it reassures a reader that what
might be an error is, in fact, what you wanted to happen.
> Second one is a more active reason. It's for calling a destructor of a
> temporary object immediately. When the calling sequence is very important,
> it is useful. (I got it now!)
No -- the cast to void has no effect at all on when the destructor
for a temporary is called. That is the whole point of the note.
Casting to void has no effect on the semantics of the program; it
is strictly for documentation purposes.
> But, if all of reasons were above,
>
> static_cast<const void>(SomeExpression);
>
> and
>
> static_cast<void>(SomeExpression);
>
> are the same meaning ( same effect )?
> Is the cv-qualifier meaningless?
Yes. A cast to a type other than a reference type is an rvalue
(5.2.9p1), and non-class rvalues always have cv-unqualified types
(3.10p9).
--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
[ 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: m-morita@trc.rwcp.or.jp (Masao Morita)
Date: 1999/09/14 Raw View
in "static_cast to void":
> > Second one is a more active reason. It's for calling a destructor of a
> > temporary object immediately. When the calling sequence is very important,
> > it is useful. (I got it now!)
>
> No -- the cast to void has no effect at all on when the destructor
> for a temporary is called. That is the whole point of the note.
> Casting to void has no effect on the semantics of the program; it
> is strictly for documentation purposes.
Thank you.
I read 5.2.9 p4 again. I misunderstood "discarding the value" in this case.
"static_cast to void" certainly has no effect!
# My former post was wrong!
M. Morita
---
[ 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: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/14 Raw View
On 13 Sep 99 17:27:38 GMT, Masao Morita <m-morita@pdss7.trc.rwcp.or.jp> wrote:
>>>Second one is a more active reason. It's for calling a destructor of a
>>>temporary object immediately. When the calling sequence is very important,
>>>it is useful. (I got it now!)
>> Are you sure of this one?
>Please consider.
> Environment setup();
If I'm not mistaken, the quote from the standard said that the 'void' has
no effect on when the object is destroyed.
In both of the following
setup(),setup();
(void)setup(),(void)setup();
the compiler creates an Environment object, then creates a second
Environment object, then destroys this second Environment object,
then destroys the first. Is my understanding correct?
--
--------------
siemel b naran
--------------
[ 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/09/11 Raw View
In article <slrn7tgtbq.8tp.sbnaran@localhost.localdomain>, Siemel B.
Naran <sbnaran@uiuc.edu> writes
>Sometimes you write expressions that return a value, but you don't use
>this returned value. Then the compiler may give you a warning about
>the unused return value. To shut off these warnings, put a "(void)"
>at the beginning of the line. For example,
Do such compilers exist in the real world? Because if so they must be
exceptionally noisy. Strictly speaking they should warn me for:
cout << 1;
as I will have ignore the return value of:
operator << (cout, 1);
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: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/09/11 Raw View
Francis Glassborow wrote:
> In article <slrn7tgtbq.8tp.sbnaran@localhost.localdomain>, Siemel B.
> Naran <sbnaran@uiuc.edu> writes
> >Sometimes you write expressions that return a value, but you don't use
> >this returned value. Then the compiler may give you a warning about
> >the unused return value.
> Do such compilers exist in the real world?
I think that lint had this kind of annoying warnings.
I don't know any C++ compiler which does that.
--
Valentin Bonnard
[ 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: Masao Morita <m-morita@pdss7.trc.rwcp.or.jp>
Date: 1999/09/12 Raw View
Thank you for answering my question.
> > static_cast<void>(Any_Expression);
> >
> >This expression looks like meaningless.
> >Probably, the content of Any_expression would make sence in any case.
> >what sort of case?
> Generally casting to void is used to indicate that the (non-void) return
> value of a function call is being intentionally ignored (rather than
> accidentally overlooked). There are some coding standards that require
> this to be done -- that the return value of a non-void function must either
> be used or explicitly discarded by casting it to void.
[snip]
>
> struct S { ~S(); };
> S f();
> int g();
> void h() {
> int i = (f(), g()); // #1
> int j = ((void) f(), g()); // #2
> }
>
> In both #1 and #2, the temporary used for the return value of the call to
> f() is destroyed only after the call to g(); the cast to void in #2 makes
> clear that the return value of f() is being intentionally ignored, but it
> has no impact on when the temporary will be destroyed.
I am appreciated the comment.
I'm a little bit clear about "static_cast to void".
There are two practical reasons.
First one is a passive reason. It's for informing to a compliler discarding
the expression(the return value of a fnction call). As a result of this,
it makes avoiding accidental overlooking and suppresses the warning message
by a complier.
Second one is a more active reason. It's for calling a destructor of a
temporary object immediately. When the calling sequence is very important,
it is useful. (I got it now!)
But, if all of reasons were above,
static_cast<const void>(SomeExpression);
and
static_cast<void>(SomeExpression);
are the same meaning ( same effect )?
Is the cv-qualifier meaningless?
M. Morita
---
[ 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: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/12 Raw View
On 11 Sep 1999 16:38:19 GMT, Francis Glassborow
>In article <slrn7tgtbq.8tp.sbnaran@localhost.localdomain>, Siemel B.
>>Sometimes you write expressions that return a value, but you don't use
>>this returned value. Then the compiler may give you a warning about
>>the unused return value. To shut off these warnings, put a "(void)"
>>at the beginning of the line. For example,
>
>Do such compilers exist in the real world? Because if so they must be
>exceptionally noisy. Strictly speaking they should warn me for:
>
>cout << 1;
No. Note that the definition of std::ostream::operator<<(int) is
outline (ie, not inline), or it calls functions that are outline.
So the compiler must assume that the function has some side effects.
So the compiler is not justified in giving warnings.
The example I gave was
static_cast<SomeClass *>((Template*)0);
My compilers do give me warnings for failing to use the return type.
At the end of my post, I said that there might be better examples.
But I can't think of any.
--
--------------
siemel b naran
--------------
[ 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: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/12 Raw View
On 12 Sep 99 03:00:05 GMT, Masao Morita <m-morita@pdss7.trc.rwcp.or.jp> wrote:
>First one is a passive reason. It's for informing to a compliler discarding
>the expression(the return value of a fnction call). As a result of this,
>it makes avoiding accidental overlooking and suppresses the warning message
>by a complier.
Though it seems rare to me that one will ever want to use this feature.
>Second one is a more active reason. It's for calling a destructor of a
>temporary object immediately. When the calling sequence is very important,
>it is useful. (I got it now!)
Are you sure of this one?
> static_cast<const void>(SomeExpression);
> static_cast<void>(SomeExpression);
I think these are equivalent.
--
--------------
siemel b naran
--------------
[ 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: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/09/12 Raw View
In article <WwJQQZA6Jj23EwF3@robinton.demon.co.uk>,
francis@robinton.demon.co.uk says...
[ ... ]
> Do such compilers exist in the real world? Because if so they must be
> exceptionally noisy. Strictly speaking they should warn me for:
>
> cout << 1;
>
> as I will have ignore the return value of:
>
> operator << (cout, 1);
...not to mention for such things as:
x = 1;
since this yields (not returns, since there's no function call) a
value of 1...
In all honesty, I've never seen (or heard of) a compiler with such
stupid warnings, but I've seen at least one version of lint that did.
IMO, this is an example of lint at its worst: rather than getting
people to clean up their code, it actually convinced them to MESS up
their code just to shut it up.
Thankfully the standard doesn't require a diagnostic for such
situations.
--
Later,
Jerry.
The Universe is a figment of its own imagination.
[ 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: m-morita@trc.rwcp.or.jp (Masao Morita)
Date: 1999/09/09 Raw View
Hi,
I have a question about static_cast expression.
ISO/IEC 14882:1998(E) 5.2.9 static_cast paragraph 4
4 Any expression can be explicitly converted to type "cv void." The
expression value is discarded. [Note: however, if the value is in a
temporary variable, the destructor for that variable is not executed
until the usual time, and the value of the variable is preserved for
the purpose of executing the destructor. ]
I can not understand this paragraph.
Say.
static_cast<void>(Any_Expression);
This expression looks like meaningless.
Probably, the content of Any_expression would make sence in any case.
what sort of case?
If possible, please show me examples.
Thank you in advance.
M. Morita
---
[ 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/09/10 Raw View
Masao Morita wrote:
>
>
> static_cast<void>(Any_Expression);
>
> This expression looks like meaningless.
> Probably, the content of Any_expression would make sence in any case.
> what sort of case?
>
Sometimes in templates void ends up being a useful construct.
It's the same reason why void functions can return void expressions.
---
[ 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: wmm@fastdial.net
Date: 1999/09/11 Raw View
In a previous article, <m-morita@trc.rwcp.or.jp> writes:
>Hi,
> I have a question about static_cast expression.
>ISO/IEC 14882:1998(E) 5.2.9 static_cast paragraph 4
>4 Any expression can be explicitly converted to type "cv void." The
> expression value is discarded. [Note: however, if the value is in a
> temporary variable, the destructor for that variable is not executed
> until the usual time, and the value of the variable is preserved for
> the purpose of executing the destructor. ]
>
>I can not understand this paragraph.
>Say.
>
> static_cast<void>(Any_Expression);
>
>This expression looks like meaningless.
>Probably, the content of Any_expression would make sence in any case.
>what sort of case?
Generally casting to void is used to indicate that the (non-void) return
value of a function call is being intentionally ignored (rather than
accidentally overlooked). There are some coding standards that require
this to be done -- that the return value of a non-void function must either
be used or explicitly discarded by casting it to void.
It's obvious what "discarding" a built-in type means, but someone might have
questions about "discarding" a return value whose type is a class with a
destructor. One plausible guess might be that the temporary containing
the value is destroyed immediately. The note you cited is simply saying
that the normal pattern of deletion (at the end of the lexically-containing
full expression (12.2p3)) is followed for "discarded" values (and, of course,
that the value must be preserved until that point in order to be deleted,
even though it has been "discarded"). In simple expressions that doesn't
make any difference, as the two points are the same. In some expressions,
though, it could be detectable:
struct S { ~S(); };
S f();
int g();
void h() {
int i = (f(), g()); // #1
int j = ((void) f(), g()); // #2
}
In both #1 and #2, the temporary used for the return value of the call to
f() is destroyed only after the call to g(); the cast to void in #2 makes
clear that the return value of f() is being intentionally ignored, but it
has no impact on when the temporary will be destroyed.
----- Posted via NewsOne.Net: Free Usenet News via the Web -----
----- http://newsone.net/ -- Discussions on every subject. -----
NewsOne.Net prohibits users from posting spam. If this or other posts
made through NewsOne.Net violate posting guidelines, email abuse@newsone.net
---
[ 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: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/11 Raw View
On 09 Sep 99 19:44:47 GMT, Masao Morita <m-morita@trc.rwcp.or.jp> wrote:
>Say.
>
> static_cast<void>(Any_Expression);
>
>This expression looks like meaningless.
>Probably, the content of Any_expression would make sence in any case.
>what sort of case?
>
>If possible, please show me examples.
Sometimes you write expressions that return a value, but you don't use
this returned value. Then the compiler may give you a warning about
the unused return value. To shut off these warnings, put a "(void)"
at the beginning of the line. For example,
class Something { };
template <class T>
T f()
{
(void) static_cast<Something*>((T*)0);
return T();
}
The static_cast asserts that T is a class that derives publicly from
class Something. Without the "(void)" you get a warning that the
result of static_cast<Something*> is not used.
There are probably other more useful examples.
There might be a better reason for casting to void.
--
--------------
siemel b naran
--------------
---
[ 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 ]