Jan 22, 2012

[c++][NOTE] tricky const with typeid.

http://bytes.com/topic/c/answers/135768-why-rtti-cannot-tell-const-qualifier-variables

why the RTTI cannot tell the const qualifier of variables?

This is the expected result. The C++ standard explicitly
says:

The top-level cv-qualifiers of the lvalue expression or the type-id that is the
operand of typeid are always
ignored.


[Example:
class D { ... };
D d1;
const D d2;
typeid(d1) == typeid(d2); // yields true
typeid(D) == typeid(const D); // yields true
typeid(D) == typeid(d2); // yields true
typeid(D) == typeid(const D&); // yields true
—end example]

There are practical reasons for this. top-level constness is a
rather fleeting thing.

------------
Further read:
Appearing and Disappearing consts in C++

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.