简体   繁体   中英

What is the meaning of “Class<>” in C++?

I'm not new to C++ but I haven't coded much using it either. I have recently come across code that has "<>" in the variable declaration. Here is how it looks

MyClass<> *instancePtr;

I'm sure it has something to do with templates, but I can't really find anything online. Can anyone help me out with this? What does the code mean?

Any help is appreciated, Thanks.

It means that MyClass is a template and that MyClass template declaration supplies default arguments for all template parameters.

For example, if the template is declared as follows

template <typename T = int, typename U = double, int N = 5> class MyClass {
  ...
};

then

MyClass<> *instancePtr;

is equivalent to

MyClass<int, double, 5> *instancePtr;

Note that when you are referring to a template class, the <> is always required, even if there's nothing between the <> . You cannot just say

MyClass *instancePtr;

even if all template parameters have default arguments. (Some older compilers supported this incorrect usage.)

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