Topic: What does this code mean?
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/02/21 Raw View
Ziporama wrote:
...
> My(limited) understanding puts me in agreement with Charles, in that the
> expression:
> typeid(~MyClass());
> is meaningless. I believe that the destructor, however, returns nothing.....not
> even void, so that while typeid(void) may evaluate to some value, // I don't
> know that it will
> typeid(~MyClass()) should not compile, or should result in undefined behavior
> if it does compile.
> If i have missed the mark, please be so kind as to straighten me out.
wrong: typeid(~MyClass()) is equivalent to
typeid(MyClass().operator~()). Since MyClass::operator~() was declared
to return 'int', it is equivalent to typeid(int).
---
[ 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: Hyman Rosen <uunet!jyacc!hymie@ncar.UCAR.EDU>
Date: 1998/02/19 Raw View
ziporama@aol.com (Ziporama) writes:
> My(limited) understanding puts me in agreement with Charles, in that
> the expression: typeid(~MyClass()); is meaningless. I believe that
> the destructor, however, returns nothing.....not even void, so that
> while typeid(void) may evaluate to some value, // I don't know that
> it will typeid(~MyClass()) should not compile, or should result in
> undefined behavior if it does compile. If i have missed the mark,
> please be so kind as to straighten me out.
Yes, you have been fooled. Standing alone, ~MyClass() is not the
destructor of MyClass. It is operator~ applied to a temporary of
type MyClass. It would be the destructor only if it came after a
'.' or '->'.
[ 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: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/02/10 Raw View
Nandakumar Sankaran wrote:
>
> I am trying to call typeid(~MyClass()). I believe this evaluates the
> expression and then invokes typeid on the expression. But what
> expression does it evaluate to?
>
> #include <stdio.h>
> #include <typeinfo.h>
>
> class MyClass
> {
> public:
> MyClass() : i(0) { }
> ~MyClass() { --i; }
> int operator~() { return i + 1; }
> int i;
> };
>
> int main()
> {
> const type_info &info = typeid(~MyClass());
> printf("name = %s, raw_name = %s\n", info.name(), info.raw_name());
> return 0;
> }
It shouldn't actually evaluate anything. Since operator~ returns int, the
compiler should make info refer to the type_info object for "int".
--
Ciao,
Paul
[ 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/std-c++/faq.html ]
Author: ziporama@aol.com (Ziporama)
Date: 1998/02/16 Raw View
Charles Krug writes:
>
>Nandakumar Sankaran wrote:
>
>> I am trying to call typeid(~MyClass()). I believe this evaluates the
>> expression and then invokes typeid on the expression. But what
>> expression
>>
>
>You are attempting to explicitly invoke the destructor. That is a Very
>Bad
>Thing (tm). Does your comipler allow this? Why are you even attempting
>it?
>The destructor returns void, hence statement is meaninless at best, and
>unpredictable at worst.
My(limited) understanding puts me in agreement with Charles, in that the
expression:
typeid(~MyClass());
is meaningless. I believe that the destructor, however, returns nothing.....not
even void, so that while typeid(void) may evaluate to some value, // I don't
know that it will
typeid(~MyClass()) should not compile, or should result in undefined behavior
if it does compile.
If i have missed the mark, please be so kind as to straighten me out.
>What are you trying to do anyway? Perhaps you shouldn't be using
>expressions, about which, you "don't really understand the semantics"
>
>Perhaps your design requires more thought.
[ 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: Charles Krug <charles@pentek.com>
Date: 1998/02/06 Raw View
Nandakumar Sankaran wrote:
> I am trying to call typeid(~MyClass()). I believe this evaluates the
> expression and then invokes typeid on the expression. But what
> expression
>
You are attempting to explicitly invoke the destructor. That is a Very
Bad
Thing (tm). Does your comipler allow this? Why are you even attempting
it?
The destructor returns void, hence statement is meaninless at best, and
unpredictable at worst.
What are you trying to do anyway? Perhaps you shouldn't be using
expressions, about which, you "don't really understand the semantics"
Perhaps your design requires more thought.
--
Charles Krug, Jr.
[ 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/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/02/07 Raw View
dave porter wrote:
...
> The description of "typeid" doesn't say anything
> about evaluating the expression. If this seems weird,
Yes it does; it says that the expression is not evaluated unless it is
an lvalue of a polymorphic type.
...
> and the type of a destructor call is, I suppose, "void".
...
> Nandakumar Sankaran <NandakumarS@bsquare.com> wrote in article
> <6b9pu4$cnv@netlab.cs.rpi.edu>...
...
> > #include <stdio.h>
> > #include <typeinfo.h>
> >
> > class MyClass
> > {
> > public:
> > MyClass() : i(0) { }
> > ~MyClass() { --i; }
> > int operator~() { return i + 1; }
> > int i;
> > };
> >
> > int main()
> > {
> > const type_info &info = typeid(~MyClass());
> > printf("name = %s, raw_name = %s\n", info.name(), info.raw_name());
> > return 0;
> > }
The argument of the typeid is not a destructor call, because there is no
object for it to apply to. Instead, it is equivalent to
MyClass().operator~(), which is defined to have type 'int'. On most
systems, I would expect typeid(int).name() to return "int". However,
there is no raw_name() member in the new standard's type_info class.
[ 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: "dave porter" <porter@wultranet.com>
Date: 1998/02/05 Raw View
Caveat, I don't have the standard, so I'm operating
on the basis of Visual C++'s online help - a dangerous
thing to do when offering opinions in a C++ forum. :-)
The description of "typeid" doesn't say anything
about evaluating the expression. If this seems weird,
consider "sizeof". It doesn't evaluate its expression
either.
I think this phrase is germane here:
"If the expression is neither a pointer nor a reference to
a base class of the object, the result is a type_info reference
representing the static type of the expression."
and the type of a destructor call is, I suppose, "void".
dave
P.S. I won't mail this. Newsgroups are supposed
to be participatory.
--
For email, please remove the 'w' from my address. Sorry.
Nandakumar Sankaran <NandakumarS@bsquare.com> wrote in article
<6b9pu4$cnv@netlab.cs.rpi.edu>...
> I am trying to call typeid(~MyClass()). I believe this evaluates the
> expression and then invokes typeid on the expression. But what
> expression
> does it evaluate to? Infact, I dont even understand the semantics of the
> expression. What does the standard say about such a statement? Please
> respond via eMail to NandakumarS@bsquare.com since I dont read these
> newsgroups much. Thanks so much.
>
> #include <stdio.h>
> #include <typeinfo.h>
>
> class MyClass
> {
> public:
> MyClass() : i(0) { }
> ~MyClass() { --i; }
> int operator~() { return i + 1; }
> int i;
> };
>
> int main()
> {
> const type_info &info = typeid(~MyClass());
> printf("name = %s, raw_name = %s\n", info.name(), info.raw_name());
> return 0;
> }
[ 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 ]
Author: "Nandakumar Sankaran" <NandakumarS@bsquare.com>
Date: 1998/02/04 Raw View
I am trying to call typeid(~MyClass()). I believe this evaluates the
expression and then invokes typeid on the expression. But what
expression
does it evaluate to? Infact, I dont even understand the semantics of the
expression. What does the standard say about such a statement? Please
respond via eMail to NandakumarS@bsquare.com since I dont read these
newsgroups much. Thanks so much.
#include <stdio.h>
#include <typeinfo.h>
class MyClass
{
public:
MyClass() : i(0) { }
~MyClass() { --i; }
int operator~() { return i + 1; }
int i;
};
int main()
{
const type_info &info = typeid(~MyClass());
printf("name = %s, raw_name = %s\n", info.name(), info.raw_name());
return 0;
}
--
Nandakumar Sankaran
NandakumarS@bsquare.com
[ 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 ]