hu wang
hu wang

Reputation: 421

why Specialized template class need forward declaration?

such is the code:

template<typename,int> class Uoo;  //without this will result in complie error,why?

template<typename T>
class Uoo<T,1>
{
};

int main(){
    return 0;
}

why Specialized template class need forward declaration?

Upvotes: 2

Views: 3264

Answers (3)

Lorenzo Gatti
Lorenzo Gatti

Reputation: 1270

As declared, template<typename T> class Uoo<T,1> is a partial specialization of template<typename,int> class Uoo; it fixes the int parameter to 1. It cannot be a partial specialization of a template that doesn't exist.

You could make your "real" class template self-sufficient by writing

template<typename T>
class Uoo
{
...
};

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234785

The following code is a specialisation of a template.

template<typename T>
class Uoo<T,1>
{
};

But you haven't said what the unspecialised form is, and the language requires you to do that. So you need to add the prototype:

template<typename,int> class Uoo;

You don't actually need to declare the unspecialised form since an instance of it is never required. So a prototype is sufficient.

Upvotes: 2

wheaties
wheaties

Reputation: 35980

It's not actually a forward declaration that you're making. What you are doing is first defining the "pattern" of the templated class and then you're defining a specialized context or version of it. The better question is, if you didn't have a non-specialized case, then what would be the point of the 2nd template parameter?

Upvotes: 0

Related Questions