“If constexpr” in C++17 does not work in a non-templated function

前端 未结 3 1214
别跟我提以往
别跟我提以往 2020-11-28 13:48

I tried to play with the C++17 standard. I tried to use one of the features of C++17 if constexpr. And I had a problem... Please take a look at the following co

3条回答
  •  心在旅途
    2020-11-28 14:17

    C++ standard, clause 9.4.1:

    If the if statement is of the form if constexpr, the value of the condition shall be a contextually converted constant expression of type bool (8.6); this form is called a constexpr if statement. If the value of the converted condition is false, the first substatement is a discarded statement, otherwise the second substatement, if present, is a discarded statement. During the instantiation of an enclosing templated entity (Clause 17), if the condition is not value-dependent after its instantiation, the discarded substatement (if any) is not instantiated.

    (emphasis mine)

    So, a substatement of a constexpr if still gets instantiated if it is not inside a template, thus it must at least compile.

提交回复
热议问题