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 > template member specialization of template class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
template member specialization of template class

Réponse
 
LinkBack Outils de la discussion
Vieux 22/02/2008, 07h10   #1
toton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut template member specialization of template class

Hi,
How can I specialize a template member function for a template class.
eg, I have a tuple like class
enum dir_type{
dir_x,dir_y
};
template< typename C>
class pair_type{
typedef C value_type;
typedef pair_type<C> self_type;
private:
value_type x_;
value_type y_;
public:
pair_type(const value_type& x,const value_type& y) : x_(x),y_(y){}
template<dir_type d>
value_type& get();

};

and want to call like
typedef pair_type<int> PT;
PT p(10,12);
int x = p.get<dir_x>();

now I want to specialize template<dir_type d> value_type& get() for
dir_x and dir_y, without specializing class pair_type for C.
1) is it possible ? if, then what is the syntax ?
2) if possible, can I write such specializations in the header file
directly ?
3) if not possible, in which way I can do this (like the way boost
tuple returns, only instead of get<1> etc, I want to have the enums as
the name i.e get<dir_x>()

any is appreciated.
thanks
abir

  Réponse avec citation
Vieux 22/02/2008, 07h41   #2
Ondra Holub
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template member specialization of template class

On 22 Ún, 08:10, toton <abirba...@gmail.com> wrote:
> Hi,
> How can I specialize a template member function for a template class.
> eg, I have a tuple like class
> enum dir_type{
> dir_x,dir_y};
>
> template< typename C>
> class pair_type{
> typedef C value_type;
> typedef pair_type<C> self_type;
> private:
> value_type x_;
> value_type y_;
> public:
> pair_type(const value_type& x,const value_type& y) : x_(x),y_(y){}
> template<dir_type d>
> value_type& get();
>
> };
>
> and want to call like
> typedef pair_type<int> PT;
> PT p(10,12);
> int x = p.get<dir_x>();
>
> now I want to specialize template<dir_type d> value_type& get() for
> dir_x and dir_y, without specializing class pair_type for C.
> 1) is it possible ? if, then what is the syntax ?
> 2) if possible, can I write such specializations in the header file
> directly ?
> 3) if not possible, in which way I can do this (like the way boost
> tuple returns, only instead of get<1> etc, I want to have the enums as
> the name i.e get<dir_x>()
>
> any is appreciated.
> thanks
> abir


Hi.

It would be something like

template<typename C>
template<>
pair_type<C>::value_type& get<dir_x>()
{
}

However I think it is not possible. As workaround you can create some
structure containing static method providing required functionality. I
think example is better:

enum dir_type
{
dir_x,dir_y
};

template< typename C>
class pair_type;

template<typename C, dir_type d>
struct GetImplementation
{
static inline typename pair_type<C>::value_type& get()
{
// Default implementation
}
};

template<typename C>
struct GetImplementation<C, dir_x>
{
static inline typename pair_type<C>::value_type& get()
{
// Specialized implementation
}
};

template< typename C>
class pair_type
{
public:
typedef C value_type;
typedef pair_type<C> self_type;
private:
value_type x_;
value_type y_;
public:
pair_type(const value_type& x,const value_type& y) : x_(x),y_(y){}

template<dir_type d>
value_type& get()
{
return GetImplementation<C, d>::get();
}

};
  Réponse avec citation
Vieux 22/02/2008, 12h28   #3
toton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template member specialization of template class

On Feb 22, 12:41 pm, Ondra Holub <ondra.ho...@post.cz> wrote:
> On 22 Ún, 08:10, toton <abirba...@gmail.com> wrote:
>
>
>
> > Hi,
> > How can I specialize a template member function for a template class.
> > eg, I have a tuple like class
> > enum dir_type{
> > dir_x,dir_y};

>
> > template< typename C>
> > class pair_type{
> > typedef C value_type;
> > typedef pair_type<C> self_type;
> > private:
> > value_type x_;
> > value_type y_;
> > public:
> > pair_type(const value_type& x,const value_type& y) : x_(x),y_(y){}
> > template<dir_type d>
> > value_type& get();

>
> > };

>
> > and want to call like
> > typedef pair_type<int> PT;
> > PT p(10,12);
> > int x = p.get<dir_x>();

>
> > now I want to specialize template<dir_type d> value_type& get() for
> > dir_x and dir_y, without specializing class pair_type for C.
> > 1) is it possible ? if, then what is the syntax ?
> > 2) if possible, can I write such specializations in the header file
> > directly ?
> > 3) if not possible, in which way I can do this (like the way boost
> > tuple returns, only instead of get<1> etc, I want to have the enums as
> > the name i.e get<dir_x>()

>
> > any is appreciated.
> > thanks
> > abir

>
> Hi.
>
> It would be something like
>
> template<typename C>
> template<>
> pair_type<C>::value_type& get<dir_x>()
> {
>
> }
>
> However I think it is not possible. As workaround you can create some
> structure containing static method providing required functionality. I
> think example is better:
>
> enum dir_type
> {
> dir_x,dir_y
>
> };
>
> template< typename C>
> class pair_type;
>
> template<typename C, dir_type d>
> struct GetImplementation
> {
> static inline typename pair_type<C>::value_type& get()
> {
> // Default implementation
> }
>
> };
>
> template<typename C>
> struct GetImplementation<C, dir_x>
> {
> static inline typename pair_type<C>::value_type& get()
> {
> // Specialized implementation
> }
>
> };
>
> template< typename C>
> class pair_type
> {
> public:
> typedef C value_type;
> typedef pair_type<C> self_type;
> private:
> value_type x_;
> value_type y_;
> public:
> pair_type(const value_type& x,const value_type& y) : x_(x),y_(y){}
>
> template<dir_type d>
> value_type& get()
> {
> return GetImplementation<C, d>::get();
> }
>
> };


Hi thanks for the answer.
However I am still confused how to use it .
two problems i am facing in the implementation,
1) GetImplementation is called on type , so how the get will return
the value , i.e x_ (which is also private, need to use some friend
access ? )
2) I dont have a common implementation for template<typename C,
dir_type d> struct GetImplementation
so how to get rid of that & only have the partial specializations ?

thanks
abir
  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 07h54.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11805 seconds with 11 queries