1

I was reading cppreference page for If statements, as well as C++17 standard (draft) and I also found out a (non exhaustive) pre-existing question on stackoverflow.

What I understand is that, within a template, a discarded sub-statement of an if constexpr is not instantiated, thereby not checked (unless the condition remains value-dependent, but let me suppose we are not in such a case).

Now, from both the C++17 standard (in §17.7, clause 8) and the current draft standard (in §13.8.1, clause 6) I read:

§13.8.1, clause 6 : The program is ill-formed, no diagnostic required, if
§13.8.1, clause (6.1): no valid specialization ... can be generated for a template or a substatement of a constexpr if statement (9.4.1) within a template and the template is not instantiated, or
...

§13.8.1: [Note 5: If a template is instantiated, errors will be diagnosed according to the other rules in this document. Exactly when these errors are diagnosed is a quality of implementation issue. — end note ]

What the note seems reading to me is that "and the template is not instantiated" is added just because, in case of instantiations, there will be compiling errors as stated everywhere else in the standard.

Does all that mean that it is possible to have a sub-statement of an if constexpr containing code that cannot compile regardless of the template parameter ?
For example, with:

template<typename T>
void f(){
    if constexpr(true){

    } else{
        char *p;
        float f;
        p = f;
    }
}

(1) What should a compiler do in this case ?

(2) Will the behaviour be different with or without instantiations ?

(3) Why on godbolt I see compile error using clang but not with gcc ?

(4) Last, but not least, I see that cppreference refers CWG2518 with a proposal to allow having static_assert(false, ...) in the discarded sub-statement of an if constexpr -> check in particular the final part for "P2593R1 (static_assert(false))".
Reading the proposal generated several doubts about what should be the correct answer to previous questions. For example, if the answers to (1)/(2) are 'never error', then why we would need a specific proposal to allow static_assert(false) ?
On the other hand, if the answers to (1)/(2) are 'always error', then I am confused why the proposal does not contain special rules even for the case of template instantiations instead of only in the clause (6.1) that reads "and the template is not instantiated".

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • The clause "and the template is not instantiated" in [6.1](https://eel.is/c++draft/temp.res#general-6.1) doesn't make a lot of sense to me. I think it is an editorial error. – n. m. could be an AI Jun 19 '23 at 17:58
  • As for different behaviour of compilers, "no diagnostic required" means just that. One compiler chooses to issue a diagnostic, although it is not required. The other one doesn't. Both are right. – n. m. could be an AI Jun 19 '23 at 18:10
  • ok for "no diagnostic required" for question (3). About "and the template is not instantiated", I see it related with the note "If a template is instantiated, errors will be diagnosed according to the other rules in this document...", thereby it should not be an editorial error. Going back to the example: (5) is the else sub-statement discarded, hence not instantiated (as in a template), thereby not checked ? (6) if it is really not checked, why we should fall in the clause (6.1) so that clang complains ? I have the feeling of some ambiguities. – user20575107 Jun 20 '23 at 07:08
  • "If a template is instantiated, errors will be diagnosed according to the other rules in this document..." This *is* an error, because even if the template is instantiated, constexpr if substatements need not be, so errors in those are not necessarily diagnosed. "I have the feeling of some ambiguities." Nothing wrong with your feeling, I don't think the standard language is clear either. – n. m. could be an AI Jun 20 '23 at 07:16
  • I often have the feeling of lack of clearness, but I generally find a kind of consistency. I see consistency when clause 6.1 reads "no valid specialization ... can be generated for a template", that is only when a 'wrong' substatement is for sure not discarded (thereby clang wrong for my example). On the other hand, the part "or a substatement of a constexpr if statement within a template" in my opinion means that, when there is no instantiation, it doesn't matter whether it is discarded or not (and the note should avoid giving the feeling of equivalent behavior with/without instantiations). – user20575107 Jun 20 '23 at 07:51
  • As a consequence, my guess is that "or a substatement of a constexpr if statement within a template" should be removed (or elaborated to follow the section "if statement") and clang should be fixed, not complaining for that example. This would also avoid the need to add ", ignoring static_assert-declarations that fail, " in the proposal from https://cplusplus.github.io/CWG/issues/2518.html. Do you see any different way to reach consistency ? – user20575107 Jun 20 '23 at 07:54
  • I cannot tell what exactly should be fixed (if anything --- perhaps I misunderstand something in the language). – n. m. could be an AI Jun 20 '23 at 08:35
  • ok, thanks. I also see this related question (https://stackoverflow.com/questions/56334957/does-if-constexprsomething-false-always-omit-template-instantiation) but it seems not solve my doubts. – user20575107 Jun 20 '23 at 08:45

0 Answers0