Extracting template types from a typedef'd template declaration

Discussion in 'C++' started by Adam Nielsen, Oct 18, 2007.

  1. Adam Nielsen

    Adam Nielsen Guest

    Hi everyone,

    Yet another syntax problem that's baffling me with templates. I want to
    instantiate a template with a single parameter as per normal, however
    the parameter is actually a template class itself, with all *its*
    parameters filled out (in the form of a typedef.) I can't work out how
    to break apart the typedef to reveal what data types were used to create
    it in the first place.

    Here is some example code that demonstrates the problem. I've tried
    every syntax I can think of but I haven't managed to hit upon the right
    one yet! Any suggestions would be much appreciated.

    Many thanks as always,
    Adam.


    #include <iostream>

    template <class TFrom, class TTo>
    class CLinkData;

    class A { };
    class B { };

    typedef CLinkData<A, B> MyLink;

    // How do you declare the template to accept a CLinkData parameter, but
    // broken out into its component types?
    //template <template <class TLinkFrom, class TLinkTo> class TLinkData>
    //template <template <class TLinkFrom, class TLinkTo>
    // class TLinkData<TLinkFrom, TLinkTo> >
    template <class CLinkData<TLinkFrom, TLinkTo> >
    void createStoredLink(void)
    {
    std::cerr << "CLinkData types are " << typeid(TLinkFrom).name() <<
    " and " << typeid(TLinkTo).name() << std::endl;
    // TLinkFrom should be A, and TLinkTo should be B, the types used
    // to declare MyLink.
    return;
    }

    int main(void)
    {
    createStoredLink<MyLink>();
    return 0;
    }
     
    Adam Nielsen, Oct 18, 2007
    #1
    1. Advertising

  2. Adam Nielsen

    Guest

    On Oct 18, 4:40 am, Adam Nielsen <>
    wrote:
    > Hi everyone,
    >
    > Yet another syntax problem that's baffling me with templates. I want to
    > instantiate a template with a single parameter as per normal, however
    > the parameter is actually a template class itself, with all *its*
    > parameters filled out (in the form of a typedef.) I can't work out how
    > to break apart the typedef to reveal what data types were used to create
    > it in the first place.
    >
    > Here is some example code that demonstrates the problem. I've tried
    > every syntax I can think of but I haven't managed to hit upon the right
    > one yet! Any suggestions would be much appreciated.
    >
    > Many thanks as always,
    > Adam.
    >
    > #include <iostream>
    >
    > template <class TFrom, class TTo>
    > class CLinkData;
    >
    > class A { };
    > class B { };
    >
    > typedef CLinkData<A, B> MyLink;
    >
    > // How do you declare the template to accept a CLinkData parameter, but
    > // broken out into its component types?
    > //template <template <class TLinkFrom, class TLinkTo> class TLinkData>
    > //template <template <class TLinkFrom, class TLinkTo>
    > // class TLinkData<TLinkFrom, TLinkTo> >
    > template <class CLinkData<TLinkFrom, TLinkTo> >
    > void createStoredLink(void)
    > {
    > std::cerr << "CLinkData types are " << typeid(TLinkFrom).name() <<
    > " and " << typeid(TLinkTo).name() << std::endl;
    > // TLinkFrom should be A, and TLinkTo should be B, the types used
    > // to declare MyLink.
    > return;
    >
    > }
    >
    > int main(void)
    > {
    > createStoredLink<MyLink>();
    > return 0;
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -


    Hi,
    try one of the following.
    Bye,
    Francesco B.

    #include <iostream>
    #include <typeinfo>

    template< typename T >
    struct CTypeFromType
    {};

    template <class TFrom, class TTo>
    class CLinkData;


    class A { };
    class B { };


    typedef CLinkData<A, B> MyLink;

    // first method

    template< typename TFrom, typename TTo >
    void createStoredLinkAux( CTypeFromType< CLinkData< TFrom, TTo > > )
    {
    std::cerr << "CLinkData types are " << typeid(TFrom).name() <<
    " and " << typeid(TTo).name() << std::endl;

    return;



    }

    template < typename T >
    inline void createStoredLink(void)
    {
    createStoredLinkAux( CTypeFromType< T >() );
    }

    // second method

    template< typename T >
    struct CCreateStoredLink
    {};

    template< typename TFrom, typename TTo >
    struct CCreateStoredLink< CLinkData< TFrom, TTo > >
    {
    static void Do()
    {
    std::cerr << "CLinkData types are " << typeid(TFrom).name() <<
    " and " << typeid(TTo).name() << std::endl;

    return;
    }
    };

    int main(void)
    {
    createStoredLink<MyLink>();
    CCreateStoredLink<MyLink>::Do();
    //createStoredLink< int >(); //compile time error
    //CCreateStoredLink< int>::Do(); // compile tiem error
    std::cin.get();
    return 0;
    }
     
    , Oct 18, 2007
    #2
    1. Advertising

  3. Adam Nielsen

    Adam Nielsen Guest

    > Hi,
    > try one of the following.
    > Bye,
    > Francesco B.
    >
    > // first method
    >


    I like this one the best, I'll use that. Thanks for your help! That
    CTypeFromType idea is turning out to be quite versatile :)

    Cheers,
    Adam.
     
    Adam Nielsen, Oct 19, 2007
    #3
  4. Adam Nielsen

    Guest

    On Oct 19, 2:34 am, Adam Nielsen <>
    wrote:
    > > Hi,
    > > try one of the following.
    > > Bye,
    > > Francesco B.

    >
    > > // first method

    >
    > I like this one the best, I'll use that. Thanks for your help! That
    > CTypeFromType idea is turning out to be quite versatile :)
    >
    > Cheers,
    > Adam.


    Have a look at Alexandrescu's "Modern C++ Design" Techniques chapter.
    There are quite a few interesting things in there.

    Most probably there are many other books like that, but I don't have
    much time to read...
    ;-)
    Bye,
    FB
     
    , Oct 19, 2007
    #4
    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. Vu Pham
    Replies:
    16
    Views:
    555
    Nick Hounsome
    Jan 14, 2004
  2. Jordi Vilar
    Replies:
    5
    Views:
    2,286
    Jonathan Turkanis
    Feb 18, 2004
  3. Replies:
    2
    Views:
    439
    Aleksey Loginov
    Nov 7, 2005
  4. oor
    Replies:
    0
    Views:
    1,357
  5. shaan
    Replies:
    2
    Views:
    544
    shaan
    Jul 2, 2012
Loading...

Share This Page