constexpr if and static_assert

前端 未结 4 2097
清酒与你
清酒与你 2020-11-27 06:39

P0292R1 constexpr if has been included, on track for C++17. It seems useful (and can replace use of SFINAE), but a comment regarding static_assert being ill

4条回答
  •  半阙折子戏
    2020-11-27 07:33

    Your self-answer and possibly the one by T.C. are not quite correct.

    First of all, the sentence "Both two static asserts will fire even though within if constexpr" is not correct. They won't because the if constexpr condition depends on a template parameter.
    You can see that if you comment out the static_assert(false) statements and the definition of buzz() in your example code: static_assert(!IntCase) won't fire and it will compile.

    Furthermore, things like AlwaysFalse::value or ! std::is_same_v are allowed (and have no effect) inside a discarded constexpr if, even if there's no T for which they evaluate to true.
    I think that "no valid specialization can be generated" is bad wording in the standard (unless cppreference is wrong; then T.C. would be right). It should say "could be generated", with further clarification of what is meant by "could".

    This is related to the question whether AlwaysFalse::value and ! std::is_same_v are equivalent in this context (which is what the comments to this answer are about).
    I would argue that they are, since it's "can" and not "could" and both are false for all types at the point of their instantiation.
    The crucial difference between std::is_same and the non-standard wrapper here is that the latter could theoretically be specialized (thanks, cigien, for pointing this out and providing the link).

    The question whether ill-formed NDR or not also crucially depends on whether the template is instantiated or not, just to make that entirely clear.

提交回复
热议问题