[英]Does default argument of template class, instantiate the template?
Does the following default argument for the template instantiates a template with type EmptyClass
? 模板的以下默认参数是否实例化类型为
EmptyClass
的模板?
class EmptyClass{};
template <typename TYPE=EmptyClass>
class Sample
{
public:
static void test()
{
TYPE::Serialize();
}
};
No. Template are instantiated when used, and are instantiated on a per-function base. 否。模板在使用时被实例化,并基于每个功能实例化。
Default parameter values are just the types to use when the parameter is not specified. 默认参数值只是未指定参数时要使用的类型。 But does not themselves imply usage.
但是,它们本身并不暗示使用。
When you call Sample<>::test()
then Sample<Emptyclass>::test()
is instantiated and the EmptyClass::serialize()
call attempted, resulting in a compile-time error (Since Emptyclass
is declared as not having such function) 当您调用
Sample<>::test()
将实例化Sample<Emptyclass>::test()
并尝试调用EmptyClass::serialize()
,从而导致编译时错误(因为Emptyclass
被声明为不具有此类功能)
Try to make up more function, containing different compile-time errors referring to different parameter, and you'll see how, until the function is not used, no error is produced. 尝试组成更多函数,其中包含涉及不同参数的不同编译时错误,您将了解如何使用该函数,直到不使用该函数,才不会产生错误。
No, in that code any instance of EmptyClass is created. 不,在该代码中将创建EmptyClass的任何实例。 Serialize is a static function.
序列化是一个静态函数。 And EmptyClass's constructor is never called (in code showed)
而且从不调用EmptyClass的构造函数(在显示的代码中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.