Does casting to a pointer to a template instantiate that template?

后端 未结 2 1693
谎友^
谎友^ 2020-12-29 22:07

static_cast*>(0) - does this instantiate the_template with type int?

The reason for asking is th

相关标签:
2条回答
  • 2020-12-29 22:50

    n3242 §14.7.1/1

    Unless a class template specialization has been explicitly instantiated (14.7.2) or explicitly specialized (14.7.3), the class template specialization is implicitly instantiated when the specialization is referenced in a context that requires a completely-defined object type or when the completeness of the class type affects the semantics of the program. The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations, but not of the definitions or default arguments, of the class member functions, member classes, static data members and member templates; and it causes the implicit instantiation of the definitions of member anonymous unions. Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.

    It seems to me that static_cast would require the instantiation of the declarations, but not of the definitions (as you are just dealing with pointers).

    n3242 §14.6.5/1

    Friend classes or functions can be declared within a class template. When a template is instantiated, the names of its friends are treated as if the specialization had been explicitly declared at its point of instantiation.

    I think that it should link, but maybe someone else can interpret better than I can.

    0 讨论(0)
  • 2020-12-29 23:09

    It is not the cast that instantiates the class template specialization, but the function call, because the argument triggers ADL . The instantiation is done because the completeness of it may affect the semantics of the program.

    That clang does not follow the spec here is known and a PR was sent by me some time ago. See http://llvm.org/bugs/show_bug.cgi?id=9440

    0 讨论(0)
提交回复
热议问题