473,419 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

typeid ... more


I was trying to garner a feel for typeid and it's use with polymorphic
types

#include <iostream>

template <class T>
bool is_polymorphic() {
bool result(false);
typeid( (result=true), *(T*)0);
return result;
}

struct non_polymorphic {};
struct polymorphic { virtual ~polymorphic() {} };
int main() {
std::cout << is_polymorphic<int>() << '\n';
std::cout << is_polymorphic<non_polymorphic>() << '\n';
std::cout << is_polymorphic<polymorphic>() << '\n'; //
3
}

..NET throws an bad_typeid exception when it encounters the line ' //3.
The exception:

// file dbgheap.c
pvBlk = _heap_alloc_dbg(nSize, nBlockUse, szFileName, nLine);

Now the use of typeid " typeid( (result=true), *(T*)0); " - seems
funny to me so I'm still trying to understand it, nonetheless the
question: Why would I get a bad_typeid exception here?
Perhaps OT hence my apologies upfront.
The entire boost library if memory serves is 7 mebibytes when built.
It's appears that because of all the dependencies, it's not possible
for me to pick out piece parts - for instance the serialization (at
present that's all I'm interested in using) library. Am I incorrect on
this?

Dec 17 '05 #1
6 2075
ma740988 wrote:
I was trying to garner a feel for typeid and it's use with polymorphic
types

#include <iostream>

template <class T>
bool is_polymorphic() {
bool result(false);
typeid( (result=true), *(T*)0);
return result;
}

struct non_polymorphic {};
struct polymorphic { virtual ~polymorphic() {} };
int main() {
std::cout << is_polymorphic<int>() << '\n';
std::cout << is_polymorphic<non_polymorphic>() << '\n';
std::cout << is_polymorphic<polymorphic>() << '\n'; //
3
}

.NET throws an bad_typeid exception when it encounters the line ' //3.
The exception:

// file dbgheap.c
pvBlk = _heap_alloc_dbg(nSize, nBlockUse, szFileName, nLine);

Now the use of typeid " typeid( (result=true), *(T*)0); " - seems
funny to me so I'm still trying to understand it, nonetheless the
question: Why would I get a bad_typeid exception here?


Works well on Comeau online, must be the compiler's internal error.

Few days ago, Alf wrote a very informative reply explaining the
behaviour of such a function. It can be found at
http://groups.google.co.in/group/com...cc1fde5e0e17a3

Dec 17 '05 #2
In article <11**********************@o13g2000cwo.googlegroups .com>,
"Neelesh Bodas" <ne***********@gmail.com> wrote:
ma740988 wrote:
I was trying to garner a feel for typeid and it's use with polymorphic
types

#include <iostream>

template <class T>
bool is_polymorphic() {
bool result(false);
typeid( (result=true), *(T*)0);
return result;
}

struct non_polymorphic {};
struct polymorphic { virtual ~polymorphic() {} };
int main() {
std::cout << is_polymorphic<int>() << '\n';
std::cout << is_polymorphic<non_polymorphic>() << '\n';
std::cout << is_polymorphic<polymorphic>() << '\n'; //
3
}

.NET throws an bad_typeid exception when it encounters the line ' //3.
The exception:

// file dbgheap.c
pvBlk = _heap_alloc_dbg(nSize, nBlockUse, szFileName, nLine);

Now the use of typeid " typeid( (result=true), *(T*)0); " - seems
funny to me so I'm still trying to understand it, nonetheless the
question: Why would I get a bad_typeid exception here?


Works well on Comeau online, must be the compiler's internal error.

Few days ago, Alf wrote a very informative reply explaining the
behaviour of such a function. It can be found at
http://groups.google.co.in/group/com...cc1fde5e0e17a3


In Addition to Alf's good comments, 5.2.8p2 specifically mentions that
if the expression in a typeid is a polymorphic class type obtained from
dereferencing a pointer, and that pointer is null, the typeid expression
throws a bad_typeid. Sounds like .NET is spot on.

-Howard
Dec 17 '05 #3
Neelesh, Howard, thanks for the comment.

Neelesh, from the looks of Howards comment. It sounds like something
is amiss about Comeau (though I haven't tried it).

|| Sounds like .NET is spot on.
OK!!
template<class T> bool is_polymorphic( const T&r )
{
bool result = false;
typeid(result = true, r);
return result;
}


The thing that puzzles me when I review this line: " typeid(result =
true, r); " is the fact that the expression makes no use of the the
type being evaluated. Am I reading this wrong?

For instance here I make use of the type within typeid.
class any
{
void* obj;
std::type_info type;
public:
template <typename T>
any(const T& t)
{
obj = (void*)(new T(t));
type = typeid(T); //<------- NOTE: type T here
}

template <typename T>
T& whatever_cast()
{
if(typeid(T) != type) //<------- ditto
throw bad_whatever_cast;
return *(T*)obj;
}
};

Dec 17 '05 #4
In article <11**********************@g47g2000cwa.googlegroups .com>,
"ma740988" <ma******@gmail.com> wrote:
Neelesh, Howard, thanks for the comment.

Neelesh, from the looks of Howards comment. It sounds like something
is amiss about Comeau (though I haven't tried it).


Comeau online doesn't execute, it only compiles. So I wouldn't expect
it to throw any exceptions.
template<class T> bool is_polymorphic( const T&r )
{
bool result = false;
typeid(result = true, r);
return result;
}


The thing that puzzles me when I review this line: " typeid(result =
true, r); " is the fact that the expression makes no use of the the
type being evaluated. Am I reading this wrong?


It's time to plunk down $18 US for a pdf of the standard. Not so that
you can read it cover to cover, but so that you can look things up:

http://webstore.ansi.org/

5.2.8p3 says that if the typeid expression refers to something other
than an lvalue of a polymorphic type, it is not evaluated. So when "r"
is not polymorphic, "result=true" is never executed. It's a pretty cute
trick.

Note that Alf showed how this information (is_polymorphic) can usually
be obtained at compile time (as opposed to run time). Indeed, boost has
a compile time is_polymorphic. And is_polymorphic has subsequently been
voted into the first library technical report and may already be in
namespace std::tr1 in your tool set. I am hopeful that it will be fully
standardized and placed into namespace std for C++0X.

In gcc (4.0 and later) this prints out:

#include <iostream>
#include <typeinfo>
#include <tr1/type_traits>

struct non_polymorphic {};
struct polymorphic { virtual ~polymorphic() {} };

int main() {
std::cout << std::tr1::is_polymorphic<int>::value << '\n';
std::cout << std::tr1::is_polymorphic<non_polymorphic>::value << '\n';
std::cout << std::tr1::is_polymorphic<polymorphic>::value << '\n';
}

0
0
1

The cool thing about this being a compile time value is that you can
make compile time decisions with this knowledge (such as select
different algorithms based on compile time polymorphism).

#include <iostream>
#include <typeinfo>
#include <tr1/type_traits>

struct non_polymorphic {};
struct polymorphic { virtual ~polymorphic() {} };

void test(std::tr1::true_type)
{
std::cout << "I'm polymorphic\n";
}

void test(std::tr1::false_type)
{
std::cout << "I'm not polymorphic\n";
}

int main() {
test(std::tr1::is_polymorphic<int>());
test(std::tr1::is_polymorphic<non_polymorphic>());
test(std::tr1::is_polymorphic<polymorphic>());
}

I'm not polymorphic
I'm not polymorphic
I'm polymorphic

No virtual function calls above. Overload resolution binds things at
compile time. When test() is inlined, that can make a world of
difference (but I digress...).

-Howard
Dec 17 '05 #5

Howard,

|| So when "r" is not polymorphic, "result=true" is never executed.
|| It's a pretty cute trick.

Lost sight of r for a minute ... Thanks.. I'm with you now.

Dec 18 '05 #6

ma740988 wrote:
Neelesh, Howard, thanks for the comment.

Neelesh, from the looks of Howards comment. It sounds like something
is amiss about Comeau (though I haven't tried it).


Comeau online doesnot execute, it just compiles.
I was under impression that your compiler is giving compile-time error.
Missed that bad_typeid part. Apologies.

Dec 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jamie Burns | last post by:
Hello, I just did a simple benchmark: for (xx=0;xx<100000;xx++) { rDerived* derived = dynamic_cast<rDerived*>(object); if (derived) derived->setValue(message.data.messageSetInt.value); } ...
3
by: Mike | last post by:
I want to use typeid() in a base class function to determine the name of the derived class. typeid(this) returns the name of the base class (which is an abstract class) rather than the derived...
17
by: Michael Olea | last post by:
Sometimes, in an introspective mood, my code reflecting on itself, I like to write test code that prints the results of introspection, some of which consist of the names of Types. The problem is:...
1
by: Gil Grissom | last post by:
Hi, I have a problem with Qt and the use of the rtti features. I compiled Qt and the included examples with the intel c++ compiler 8.1 like the following commands show: icl -c -nologo...
18
by: Adam Zimny | last post by:
This is fragment of code from Bruce Eckel's Thinking in c++ ( last 3 couts are mine to show what happened ). The question is: is Bruce Eckel wrong or g++ ( my version is 3.2.3 ) is buggy ? //:...
4
by: ezelasky | last post by:
I am programming in microsoft VC++ 7.1 and get an unhandled exception when I use typeid on a deferenced pointer. So I tried the example below, from the msdn site and I am seeing the same...
2
by: Sarath | last post by:
Hello All Is it possible to overload typeid operator? In my understanding it's not possible Could you please provide more information on same? Regards, Sarath
5
by: David Portabella | last post by:
Hello, I have the following template class: ++++++++++++++++++++++ template <class Valueclass Test { public: void f() { if (typeid(Value) == typeid(string)) cout << "Value is a string" <<...
7
by: Deepak Jharodia | last post by:
I'm using a templatized class in GCC based environ template<class A, class B> class foo {... ....} F; Now I want to know that particular instance of this class was instantiated with what...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.