C++11 static_assert and template instantiation

后端 未结 5 1076
执念已碎
执念已碎 2020-11-29 03:41

In C++11, should the operation of static_assert within a template depend on whether that template has been instantiated or not? For example, with the following code

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 04:24

    The C++0x draft (N3242) says in 14.6p8:

    "If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required."

    The same words appear in the C++03 standard.

    In the case of the example from the question, no valid instantiation can be made for this template, so the quoted wording applies.

    Since no diagnostic is required, the compiler may compile the program if the template is not instantiated. Of course, if it is instantiated then the program is ill-formed, with a diagnostic required.

提交回复
热议问题