Frage

I know from this thread - template class member function only specialization that if I specialize a class template, I need to specialize all the member functions.

So, my rationale was that I would 'instantiate' a template class. (Not sure if its the right way of saying it)

Then, since this instantiated class is a full-fledged class, I can specialize a particular method.

// I have a template class like this - 
template<class T, T invalidVal, int e> class A
{ 
     static inline bool dummy(T value) 
    {
        return 0;
    }
 }

 // Now I create classes from the above template class with 
 // T=void*, invalidVal=NULL & e=0 
 // And with T=void *, invalidVal=NULL & e=1 
 // And I typedef these classes to use them later
 typedef A<void *, (void *)NULL, 0> A_0;
 typedef A<void *, (void *)NULL, 1> A_1;

 // So now, what I was expecting was that A_0 & A_1 are classes and 
 // they can have their own specialized dummy() method
 // So I tried following - 
 inline bool A_0::dummy(void *value) 
 {
     return value != invalidVal;
 }

 inline bool A_1::dummy(void *value)
 {
     return value == invalidVal;
 }

The above code worked in a Windows environment. (Visual Studio 2008)

But this specialization does not work - using g++-4.7 - on Linux. By reading through some other threads, I am also passing -std=c++11 to my g++ compilation command.

I get following 2 errors -

1. specializing member ‘A<void*, 0u, 0>::dummy’ requires ‘template<>’ syntax 
2. invalidVal was not declared in this scope

Now, error no. 1 goes away if I add a template<> before inline bool A_0::dummy(void *value) which makes me more worried, since I know the specialization did not happen as I wanted.

The error no. 2 doesn't go away.

I am getting more and more confused about template class member function specialization. What am I missing out here? Any reasons that g++ is complaining? Any ideas?

Thanks!

War es hilfreich?

Lösung

I'm not sure I what you mean by "specialization did not happen as I wanted". What exactly did you want?

In your code you are trying to perform explicit specialization of template class member without specializing the class itself. All forms of explicit specialization in C++ require template<> syntax

template<>
inline bool A_0::dummy(void *value) 
{
  // Whatever
}

template<>
inline bool A_1::dummy(void *value)
{
  // Whatever
}

That's just the way it is in C++. Just because you "hid" the specialized class names behind typedef-names does not mean that your are excused from having to use the template<> syntax.

Visual C++ apparently permits you to skip template <> part as a non-standard extension.

Additionally, the specialized versions will know nothing about template parameter names (they don't have any parameters anymore), meaning that name invalidVal is completely unknown to them. Either replace invalidVal with explicit NULL (as in explicit argument list), or make invalidVal value available through class as a constant static member

template<class T, T invalidVal, int e> class A
{ 
  static const T iv; // declaration
  ...
};

template <class T, T invalidVal, int e>
const T A<T, invalidVal, e>::iv = invalidVal; // definition

template <> inline bool A_0::dummy(void *value) 
{
   return value != iv;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top