Partial Template Specialization for Arrays

I am trying to create a template class for different data types:

template <typename TDataType>
class CBaseProperty : public CBasePropertyBase

      

now this class has a specialization for such arrays:

template <typename TDataType, u32 TSize>
class CBaseProperty<TDataType[TSize]> : public CBasePropertyBase

      

now I want to write a specialized version of a specific member function for strings:

template <> b8 CBaseProperty<string>::setValue(const void* _pVal)

      

as soon as I try to specify this element for my partial specialization of arrays for strings:

template <u32 _TSize> b8 CBaseProperty<string[TSize]>::setValue(const void* _pVal)

      

i am getting 3 errors:

Error 1 error C3860: Template argument list following the class template name must specify the parameters in the order used in the template parameter list

Error 2 of error C2995:: b8 CBaseProperty<TDataType>::setValue(const void*)

function template already defined

Error 3 of error C3855:: CBaseProperty<TDataType>

template parameter TDataType

is incompatible with declaration

What am I doing wrong? According to all my sources, my syntax is correct for partial member specialization.

Thanks for any help.

+3


source to share


2 answers


You can only specialize one member function by supplying all the template arguments (you left the length as an argument), this will force you to specify the length of the array.

So, if you want a specialized version for arrays of strings, leaving the length as a parameter, you first need to specialize the class template for the array of strings.

As I understand your post, the solution can be boiled down to the following minimal example:



template<class T>
class foo
{
    public:
    void tell() { cout << "general\n"; }
    void base() { cout << "ok"; }
};

template<class T, std::size_t N>
class foo<T[N]>
{
    public:
    void tell() { cout << "special arrays\n"; }
};

template<std::size_t N>
class foo<std::string[N]>
{
    public:
    void tell() { cout << "special string arrays\n"; }
};

template<>
void foo<std::string>::tell()
{
  cout << "string\n";   
}

int main()
{
  foo<std::string[2]> f;   
  f.tell();

  return 0;
}

      

Output:

special string arrays

+2


source


template <u32 _TSize> b8 CBaseProperty<string[TSize]>::setValue(const void* _pVal)

      

this is invalid code when a string is an alias for std :: string, because std :: string is variable in length, so you can specialize in its length at compile time

template <typename TDataType, u32 TSize>
class CBaseProperty<TDataType[TSize]> : public CBasePropertyBase

      



it might work, but it's risky because the type of std :: size_t is implementation, so if the type of your u32 (which you said in your comment is an unsigned int) is not the same as std :: size_t, it may not match.

see live example: http://ideone.com/ZH4v6x

0


source







All Articles