Finding template argument of child

Discussion in 'C++' started by gao_bolin@voila.fr, Feb 13, 2005.

  1. Guest

    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
    , Feb 13, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > 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.
    Ruslan Abdikeev, Feb 13, 2005
    #2
    1. Advertising

  3. adbarnet Guest

    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:...
    >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
    adbarnet, Feb 13, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nifsmith
    Replies:
    5
    Views:
    8,166
    Maitre Bart
    Sep 27, 2004
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,110
    David Schwartz
    Dec 9, 2003
  3. Karthik Gurusamy

    finding child cpu usage of a running child

    Karthik Gurusamy, Jan 26, 2008, in forum: Python
    Replies:
    2
    Views:
    358
    Karthik Gurusamy
    Jan 26, 2008
  4. nw
    Replies:
    0
    Views:
    312
  5. nguillot
    Replies:
    5
    Views:
    526
Loading...

Share This Page