Default template parameter partial specialization

前端 未结 3 714
心在旅途
心在旅途 2020-12-04 16:19

Please explain to me why the following piece of code complies and works perfectly. I am very confused.

#include
template

        
3条回答
  •  不思量自难忘°
    2020-12-04 17:08

    template
    class Base
    {};
    

    Here the default values/initialization for A and B have been declared respectively as int and double.

     template
     class Base 
    

    Here in class definitions, the first argument is something like a constant value(here int; why declare this way just making things complex? Better remove the first template argument) and the second template argument is B who default value is 'double'.

    Base<> base;
    

    When you create the object of the class. Although you do not specify the template arguments, the compiler takes default values of the arguments(A and B)which are 'int' and 'double' and the code executes without any errors or warnings.
    See what happens when you create the object as:
    Base b; or Base b;

提交回复
热议问题