2010-07-26 20 views
3

Je suis en train de se spécialiser le moment de fonction membre templated() uniquement (et non la classe trou) comme ceci:CPP membre spécialisation fonction

template<class Derived, class T> 
class AbstractWavelet { 
public: 
    [...] 

    template<bool useCache> 
    const typename T::scalar moment(const int i, const int j) const { 
    return abstractWaveletSpecialization<Derived, T, useCache>::moment(static_cast<const Derived*>(this), i, j); 
    } 
    template<bool useCache> 
    friend const typename T::scalar abstractWaveletSpecialization<Derived, T, useCache>::moment(const Derived* const that, const int i, const int j); 

protected: 
    // returns the jth moment of the ith scaling function 
    template<bool useCache> 
    inline const typename T::scalar momentImpl(const int j, const int i) const { 
    [...] 
    } // momentImpl 
}; 

La spécialisation réelle se produit dans une struct abstractWaveletSpecialization supplémentaire:

template<class Derived, class T, bool useCache> 
struct abstractWaveletSpecialization { 
    inline static const typename T::scalar moment(const Derived* const that, const int i, const int j) { 
    return that->momentImpl<useCache>(i,j); 
    } 
}; 


template<class Derived, class T> 
struct abstractWaveletSpecialization<Derived, T, true> { 

    typedef const std::pair<const int, const int> momentCacheKey; 
    typedef std::map<momentCacheKey, 
       const typename T::scalar> momentCacheType; 
    static momentCacheType momentCache; 


    inline static const typename T::scalar moment(const Derived* const that, const int i, const int j) { 
    momentCacheKey cacheKey(i,j); 
    typename momentCacheType::iterator idx = momentCache.find(cacheKey); 

    if (idx == momentCache.end()) 
     return that->momentImpl<true>(i, j); // COMPILE ERROR HERE 
    else 
     return momentCache[cacheKey]; 
    } 
}; 

le problème est que je ne peux pas appeler momentImpl() dans la struct abstractWaveletSpecialization spécialisée:

error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘bool’ to binary ‘operator<’ 

Mais le compilateur ne se plaint pas de l'appel de momentImpl dans la structure abstraite abstractWaveletSpecialization.

Mon approche est-elle interdite en C++? Ou y a-t-il un moyen de faire ce travail?

+0

duplication possible de [Où et pourquoi dois-je mettre les mots-clés "template" et "typename"?] (Http: //stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – ecatmur

Répondre

2

Pouvez-vous essayer that->template momentImpl<true>(i, j); s'il vous plaît? C'est une façon de dire au compilateur "Hey, la chose après -> est un appel de modèle"

+0

Cette syntaxe est bizarre. Mais c'est des travaux! – Manuel

+0

Ouais totalement inhabituel. Mais c'est ce qu'il faut quand on utilise C++ avec template avancé :-) – Scharron

+1

@Manual: Le problème est que 'A-> B B B '. Si c'est un objet, alors 'A-> B sbi