Is reading a pointer to one end at the end permitted in a constant expression when a short circuit disables its evaluation

Consider an example:

template <char>
struct foo { };

int main() {
    foo<""[0]?""[1]:'\0'>{};
}

The code compiles in both [gcc] and [clang] , but is it really so? I know that an expression ""[1]does not need to be evaluated as it shorts. But the standard is not very clear if an expression can really qualify as a basic constant expression. Relevant [expr.const] / 2 and especially the part:

If e satisfies the constraints of the main constant expression, but evaluating e will evaluate the operation with undefined behavior specified in the [library] through the [thread] of this document, it is not known whether e is the main expression of the constant.

in doubt ...

+4
2

, :

e , e undefined , [library] - [thread] , , e .

""[0]?""[1]:'\0' undefined, ""[1] . , '\0' .

, , (, C++11 5.16 Conditional operator [expr.cond] /1:

. bool ( 4). , , , . .

""[0] false , . :

false ? (1/0) : 42

, .

+6

, , :

e , , , :

, ([expr.cond]/1):

. bool. , , , . .

undefined, . , , , , .

+3

Source: https://habr.com/ru/post/1689892/


All Articles