PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Extracting template types from a typedef'd template declaration
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Extracting template types from a typedef'd template declaration

Réponse
 
LinkBack Outils de la discussion
Vieux 18/10/2007, 03h40   #1
Adam Nielsen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Extracting template types from a typedef'd template declaration

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;
}
  Réponse avec citation
Vieux 18/10/2007, 09h30   #2
xtrigger303@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extracting template types from a typedef'd template declaration

On Oct 18, 4:40 am, Adam Nielsen <adam.niel...@remove.this.uq.edu.au>
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>:o();
//createStoredLink< int >(); //compile time error
//CCreateStoredLink< int>:o(); // compile tiem error
std::cin.get();
return 0;
}

  Réponse avec citation
Vieux 19/10/2007, 01h34   #3
Adam Nielsen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extracting template types from a typedef'd template declaration

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


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

Cheers,
Adam.
  Réponse avec citation
Vieux 19/10/2007, 10h54   #4
xtrigger303@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extracting template types from a typedef'd template declaration

On Oct 19, 2:34 am, Adam Nielsen <adam.niel...@remove.this.uq.edu.au>
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 ! 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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h40.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10349 seconds with 12 queries