Evaluating strlen at compilation time?

前端 未结 3 2072
面向向阳花
面向向阳花 2020-12-11 04:22

If my code has this constexpr string

constexpr char my_str[] = \"hello\";

the type of my_str contains information

3条回答
  •  不知归路
    2020-12-11 05:12

    21.8, 1 and 2, in the standard says:

    1. Tables 74, 75, 76, 77, 78, and 79 describe headers , , , , (character conversions), and , respectively.

    2. The contents of these headers shall be the same as the Standard C Library headers , , , , and and the C Unicode TR header , respectively, with the following modifications:

    strlen is defined in in in c++. The modifications that follow do not mention strlen. From that, I would conclude that the signature in C++ must be exactly the same as it is in C, and since C does not have constexpr, it is technically non-compliant. That said, this is one of those non-compliant things that's unlikely to do any harm, beyond relying on it on one platform and then not finding it on another.

提交回复
热议问题