5

I am writing a templated class with one type paramenter and one boolean, here is the code:

template<class T, bool p = true>
class A
{
private:
    T* ptr;
public:
    A();
};


template<class T>
A<T,true>::A()
{
    ptr = 0xbaadf00d;
}

int main()
{
    A<int> obj;
    A<int, false> o;
    return(0);
}

And I am getting these compilation errors:

Error   1   error C3860: template argument list following class template name must list parameters in the order used in template parameter list tst.cpp 15
Error   2   error C2976: 'A<T,p>' : too few template arguments  tst.cpp 15

What am I doing wrong? Or is it for some reason forbidden to specialize non-type parameters partially?

At the same time if I use the boolean parameter in an if statement, I'm getting this warning:

Warning 1   warning C4127: conditional expression is constant

So I am supposed to do specializations for this kind of things...

Any help would be highly appreciated! :)

Thanks in advance!!!!

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
Serge
  • 1,027
  • 14
  • 21

2 Answers2

9

You are trying to partially specialize a method. That is not allowed. You can only partially specialize the whole class. Once you have specialized the class, you can then provide a non-specialized definition of the methods for the partially specialized class.

Here is an example of some code that might do what you want:

template<class T, bool p = true>
class A
{
private:
    T* ptr;
public:
    A();
};

template <class T>
class A<T,true> {
private:
    T* ptr;
public:
    A();
};


template <class T>
class A<T,false> {
private:
    T* ptr;
public:
    A();
};

template<class T>
A<T,true>::A()
{
    ptr = reinterpret_cast<T *>(0xbaadf00d);
}

template<class T>
A<T,false>::A()
{
    ptr = 0;
}

int main()
{
    A<int> obj;
    A<int, false> o;
    return(0);
}
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
6

You're welcome to specialize on non-type template parameters, but you can't specialize just one function of a template class. The class is the template, so the class is what you need to specialize. That is, to give a special definition of the constructor, you need to provide a special definition of the entire class.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 1
    Just to be a bit more precise: I cannot PARTIALLY-specialize just one function, full specialization works OK: – Serge Jun 21 '12 at 14:49
  • Thanks!!! Just one remark: I only cannot PARTIALLY-specialize one function in a classs - full specialization works fine - that's what dazzled me... – Serge Jun 21 '12 at 14:52