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 > Re: Static Member Function Template Specialization
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Static Member Function Template Specialization

Réponse
 
LinkBack Outils de la discussion
Vieux 15/10/2007, 14h55   #1
mike b
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static Member Function Template Specialization

On Oct 14, 12:38 pm, mike b <michaeljber...@gmail.com> wrote:
> Hello everyone, thanks in advance for your . I'm new to C++
> templates and have run into some issues using member function
> templates. I have a shared library containing templates that I'm
> trying to use from an executable, compile using gcc 4.1.2. Everything
> works fine until I try specializing one of the static member function
> templates in a non-template class. I have a feeling I'm messing up
> something obvious so before I post a bunch of code does the following
> look correct? Thanks.
>
> --- Shared Library (Arrays.h) ---
>
> namespace example {
> class Arrays {
> public:
> template<class T> static int compareItems(T *a1, T *a2, int length);
> protected:
> Arrays() {};
>
> };
>
> template<class T> int Arrays::compareItems(T *a1, T *a2, int length) {
> printf("Calling generic function.\n");
> return 1;
>
> }
>
> template<> int Arrays::compareItems<char>(char *a1, char *a2, int
> length) {
> printf("Calling char specialization.\n");
> return 0;
>
> }
> }


Sincere thanks to everyone who responded, the problem has been
solved. The issue it seems is that I was defining my member
specializations in the header but only a declaration for the
specialization is allowed in the header, the actual implementation
must go in the implementation file, .cpp. So here is the change to
make the code work:

--- Arrays.h ---

namespace example {
class Arrays : public Object {
public:
template<class T> static int compareItems(const T a1[], const
T a2[], const int length);
};

// I was defining the function here but only the declaration goes here
template<> int Arrays::compareItems(const char a1[], const char a2[],
const int length);

template<class T> int compareItems(const T a1[], const T a2[], const
int length) {
// Generic implementation goes here.
}
}


--- Arrays.cpp ---

// This was in the header file before and was causing the problems,
moving it here solved it.
template<> int hydro::Arrays::compareItems(const char a1[], const char
a2[], const int length) {
return strncmp(a1, a2, length);
}

--- END ---

  Réponse avec citation
Vieux 16/10/2007, 20h45   #2
Gianni Mariani
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static Member Function Template Specialization

mike b wrote:
....
> Sincere thanks to everyone who responded, the problem has been
> solved. The issue it seems is that I was defining my member
> specializations in the header but only a declaration for the
> specialization is allowed in the header, the actual implementation
> must go in the implementation file, .cpp.


Sounds strange. What errors were you getting ?
  Réponse avec citation
Vieux 17/10/2007, 01h34   #3
mike b
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static Member Function Template Specialization

On Oct 16, 3:45 pm, Gianni Mariani <gi4nos...@marian.ws> wrote:
> mike b wrote:
>
> ...
>
> > Sincere thanks to everyone who responded, the problem has been
> > solved. The issue it seems is that I was defining my member
> > specializations in the header but only a declaration for the
> > specialization is allowed in the header, the actual implementation
> > must go in the implementation file, .cpp.

>
> Sounds strange. What errors were you getting ?


I was getting errors about my specialization being defined multiple
times. This was with GCC 4.

  Réponse avec citation
Vieux 17/10/2007, 09h17   #4
Gianni Mariani
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static Member Function Template Specialization

mike b wrote:
> On Oct 16, 3:45 pm, Gianni Mariani <gi4nos...@marian.ws> wrote:
>> mike b wrote:
>>
>> ...
>>
>>> Sincere thanks to everyone who responded, the problem has been
>>> solved. The issue it seems is that I was defining my member
>>> specializations in the header but only a declaration for the
>>> specialization is allowed in the header, the actual implementation
>>> must go in the implementation file, .cpp.

>> Sounds strange. What errors were you getting ?

>
> I was getting errors about my specialization being defined multiple
> times. This was with GCC 4.



Declare the template as "inline" as see if you get the same errors.
  Réponse avec citation
Vieux 17/10/2007, 10h38   #5
Greg Herlihy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static Member Function Template Specialization

On Oct 17, 1:17 am, Gianni Mariani <gi4nos...@marian.ws> wrote:
> mike b wrote:
> > On Oct 16, 3:45 pm, Gianni Mariani <gi4nos...@marian.ws> wrote:
> >> mike b wrote:

>
> >>> Sincere thanks to everyone who responded, the problem has been
> >>> solved. The issue it seems is that I was defining my member
> >>> specializations in the header but only a declaration for the
> >>> specialization is allowed in the header, the actual implementation
> >>> must go in the implementation file, .cpp.
> >> Sounds strange. What errors were you getting ?

>
> > I was getting errors about my specialization being defined multiple
> > times. This was with GCC 4.

>
> Declare the template as "inline" as see if you get the same errors.


An explicit function specialization follows the same rules as any
other C++ function: in order to be defined in a header file, the
specialization must be declared "inline."

Greg


  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 21h27.


É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,10746 seconds with 13 queries