Specifying Default Template Arguments When Instantiating Class Template with Default Template Arguments
C 11 introduced default template arguments to provide a concise and convenient way to specify template arguments when instantiating a class template. However, specifying arguments for a class template with default template arguments can be confusing.
One may expect that simply declaring an instance of the class template, such as Foo me;, would suffice. However, in this case, the compiler will require the template arguments to be explicitly specified.
The correct way to instantiate a class template with default template arguments is to leave the template argument list empty. That is, instead of Foo me;, the code should be Foo<> me;. This indicates to the compiler that the default template arguments should be used for the class instantiation.
This syntax is consistent with the convention for calling a function with default arguments. For instance, a function foo with a single default argument can be called without specifying the argument as foo(). However, the argument syntax must still be present.
Note: As of C 17, it is possible to declare an instance of a class template without specifying template arguments, as in Foo me;. This behavior is a departure from earlier versions of C and should be used with caution, as it may not be supported by all compilers.
The above is the detailed content of How to Instantiate a Class Template with Default Template Arguments in C ?. For more information, please follow other related articles on the PHP Chinese website!