In the declaration “std::vector f();”, is “std::vector” an instantiation?

后端 未结 2 776
清酒与你
清酒与你 2021-02-01 06:29

The C++ Language Standard states the following concerning template components in the Standard Library:

The effects are undefined...if an incomplete type i

2条回答
  •  抹茶落季
    2021-02-01 06:53

    No, it does not instantiate the template. Mooing Duck's answer provides all the necessary quotes, but here is some analysis.

    Instantiation, by default, cannot occur if nothing exists to require a completely-defined type (§14.7.1/1). Function definitions specifically require complete types (§8.3.5/9), but the question is whether some other part of the standard also requires this for other declarations.

    But there's a special exception for definitions, which reveals that non-definition declarations really are different:

    The type of a parameter or the return type for a function definition shall not be an incomplete class type (possibly cv-qualified) unless the function definition is nested within the member-specification for that class (including definitions in nested classes defined within the class).

    What's special about function definitions inside member-specifications? Because a member-specification cannot declare the same function twice (§9.2/1), and member function bodies are processed after all member declarations (§3.3.7/1.1). Essentially, a nested member function definition is treated as a declaration during the first pass, and then a definition once the entire member-specification has been processed, and the class is complete (§9.2/2). And §8.3.5/9 specifies that an incomplete class is permissible for that first pass, but not the second.

    It's pretty onerous to perform an exhaustive, definitive search of the Standard's rules for function declarations and instantiations. But this example, although limited to member functions and the completeness of the enclosing type, can reasonably be extended to other functions and types. In any case, it's pretty good evidence of a distinction.

提交回复
热议问题