Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Finding template argument of child

Reply
Thread Tools

Finding template argument of child

 
 
gao_bolin@voila.fr
Guest
Posts: n/a
 
      02-13-2005
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

 
Reply With Quote
 
 
 
 
Ruslan Abdikeev
Guest
Posts: n/a
 
      02-13-2005
<> wrote in message
news: 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.


 
Reply With Quote
 
 
 
 
adbarnet
Guest
Posts: n/a
 
      02-13-2005
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

<> wrote in message
news: 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
finding child cpu usage of a running child Karthik Gurusamy Python 2 01-26-2008 09:03 AM
Passing template struct as an argument to a template function nifsmith C++ 5 09-27-2004 02:35 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments