473,406 Members | 2,439 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,406 software developers and data experts.

Finding template argument of child

I have the following situation:

class A
{
public:
virtual ~A() {}
};

template < typename T >
class B : public A
{
};

The problem is to find the template argument T when a function receives
only a pointer A* (and we know for sure that the A* indeed points to a
B<T>*).

One way is to dynamic_cast to a bunch of know B<T>, but this is kind of
heavy and works only for types that are explicitely taken into account.
Is there a way to retrieve type T for any T, and say print it out?

Thanks

Bolin

Jul 23 '05 #1
2 1314
<ga*******@voila.fr> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
template < typename T >
class B : public A {};

The problem is to find the template argument T when a function receives
only a pointer A* (and we know for sure that the A* indeed points to a
B<T>*).


In which context do you want to find the T?
If it is just to "say, print", then the easiest way is to define a
non-template intermediate base class which implements the necessary
interface:

#include <iostream>
#include <typeinfo>
#include <string>

struct A
{
virtual ~A() {}
};

struct B_base : A
{
std::string get_T_name() const { return vget_T_name(); }
private:
virtual std::string vget_T_name() const =0;
};

template<typename T>
struct B : B_base
{
private:
std::string vget_T_name() const { return typeid(T).name(); }
};

int main()
{
B<int> b_int;
A& a= b_int;
B_base& b_base= dynamic_cast<b_base&>( a );
std::cout << b_base.get_T_name() << std::endl;
}
Hope it helps,
Ruslan Abdikeev.
Jul 23 '05 #2
How about:
class A

{

public:

A(){}

virtual ~A() {}

virtual const type_info & getTypeInfo() const {return typeid(A); }

};

template <class T>

class B : public A

{

public:
virtual const type_info & getTypeInfo() const {return typeid(T); }

};

int main(int argc, char **argv)

{

A anA;

A* pA = new B<int>;

std::cout << anA.getTypeInfo().name() << std::endl;

std::cout << pA->getTypeInfo().name() << std::endl;

return 0;

}

regards,
Aiden

<ga*******@voila.fr> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I have the following situation:

class A
{
public:
virtual ~A() {}
};

template < typename T >
class B : public A
{
};

The problem is to find the template argument T when a function receives
only a pointer A* (and we know for sure that the A* indeed points to a
B<T>*).

One way is to dynamic_cast to a bunch of know B<T>, but this is kind of
heavy and works only for types that are explicitely taken into account.
Is there a way to retrieve type T for any T, and say print it out?

Thanks

Bolin


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #3

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

Similar topics

1
by: SpeedBump | last post by:
This is mostly just another "gee it would be nice if it had X" post. Recently I have come across two separate problems which both exhibit the need for a way to self reference when instantiating a...
3
by: Jamie Green | last post by:
Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am trying to query the following document <Root> <Child>Name</Child> <Child>John</Child> <Child>Smith</Child> <Child>23</Child>...
0
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If...
4
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... };...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
6
by: Hendrik Schober | last post by:
Hi, I have a problem with extending some existing code. In a simplified form, the problem looks like this: I have four types, A, B, C, and D. Each A refers to zero, one, or more B's and each...
1
by: Joseph Turian | last post by:
How can I specialize the value of only one template parameter? Here's the fragment of code I'd like to get working: typedef enum {START, PARENT, CHILD, END} locator_ty; template <locator_ty L,...
2
by: Imre | last post by:
Hi I'm trying to write a function template called CallF(), that has a type argument called T. It has one argument, a T*. If T has a member function F(), then CallF() should call it, otherwise it...
3
by: MarkMYoung | last post by:
I'm trying to transform an ugly Lotus Notes database into a condensed XML file for loading into another product that only needs ~25% of the data. For some reason, my template matches only work for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.