Re: Error compiling templates
On 3 Set, 09:57, ZikO <ze...@op.pl> wrote:
Commenting out the part that didn't work you specialized only
Sortable<T*>::sort() for char*.
Since you can create a Sortable<char*> class instance via the
Sortable<T*> class template, you aren't requested to create a specific
Sortable<char*> specialization.
But if you implement the above specialization, then you have to modify
Sortable<char*>::sort() - you must remove "template<>" before of it,
otherwise the compiler complains that your specialization doesn't
match any template declaration - although I'm not so sure about the
reason.
This is why I am confused here. From what I have read, we are generally
requested to use "template<>" to tell compiler about using full
specialization.
In any case, by removing "template<>" you are telling that it is not a
specialization of another template template function - as you did
before - but, instead, that it is the definition of the function that
you declared into the Sortable<char*> template class.
It seems like using specialization for specific type is actually
defining regular function. I will try to remember that =)
Thinking again is seems logic. After all, as I said, that's just a
definition of an already declared, "not specifically a template"
method. On the other hand, the "mandated" syntax seems to be different
for template classes and template methods.
For and odd example, you might try this:
-------
template<>
class Sortable<char*> : public std::vector<char*> {
public:
template<class T>
std::vector<T> convert(size_t item);
//if you omit the following, the compiler complains below
std::vector<int> convert(size_t item);
};
template<class T>
std::vector<T>
Sortable<char*>::convert(size_t item) {
std::vector<T> result;
if(item < size()) {
char* ptr = operator[](item);
while(*ptr) {
result.push_back(T(*ptr++));
}
}
return result;
}
//template<> // <--- necessary only if not declared in the class
std::vector<int>
Sortable<char*>::convert(size_t item) {
std::vector<int> result;
if(item < size()) {
char* ptr = operator[](item);
while(*ptr) {
result.push_back(int(*ptr++));
}
}
return result;
}
-------
In the case above, the commented out "template<>" line is _necessary_
only if the "int" specialization of the "convert" method is _not_
declared into the class.
If you declare it and you uncomment the above "template<>" line, then
the compiler won't complain, it won't make any difference about
"template<>" being present or not.
Seems like this works differently for class templates and for method
templates... well, whatever makes the compiler's day ;-)
When using templates, many things can be specialized or not. Actually,
some specializations aren't really required, but can come useful, for
instance, to improve performance on a specific type.
Hope this helped to clear out your confusion.
It's certainly clearer at the moment, although I really need more
practice in that area :P
Thanks Francesco.
You're welcome. Well, I need more practice too, you're not alone :-)
Cheers,
Francesco