简体   繁体   中英

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?

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.

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?

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
{
...
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM