简体   繁体   English

评估在编译时不满意?

[英]Evaluating strlen at compilation time?

If my code has this constexpr string 如果我的代码具有此constexpr字符串

constexpr char my_str[] = "hello";

the type of my_str contains information about its size, ie sizeof(my_str) is a constant 6, and can be used anywhere a constant is required. my_str的类型包含有关其大小的信息,即sizeof(my_str)是常数6,可在需要常数的任何地方使用。

What about strlen(my_str) ? 那么strlen(my_str)呢? Can/should it also be evaluated to a compile-time constant? 是否可以/也应该将其评估为编译时常数?

Here is an example for yes: https://ideone.com/2U65bN 这是yes的示例: https : //ideone.com/2U65bN

Here is an example for no: http://coliru.stacked-crooked.com/a/8cb094776dfc5969 这是一个没有的示例: http : //coliru.stacked-crooked.com/a/8cb094776dfc5969

What does the Standard say about this? 标准对此有何看法? Surely not "maybe"? 当然不是“也许”吗?

在C ++ 17中,可以使用std::char_traits::length (即constexpr )代替strlen

21.8, 1 and 2, in the standard says: 标准中的21.8、1和2表示:

  1. Tables 74, 75, 76, 77, 78, and 79 describe headers <cctype> , <cwctype> , <cstring> , <cwchar> , <cstdlib> (character conversions), and <cuchar> , respectively. 表74、75、76、77、78和79分别描述了标头<cctype><cwctype><cstring><cwchar><cstdlib> (字符转换)和<cuchar>

  2. The contents of these headers shall be the same as the Standard C Library headers <ctype.h> , <wctype.h> , <string.h> , <wchar.h> , and <stdlib.h> and the C Unicode TR header <uchar.h> , respectively, with the following modifications: 这些标头的内容应与标准C库标头<ctype.h><wctype.h><string.h><wchar.h><stdlib.h>以及C Unicode TR相同标头<uchar.h> ,分别进行了以下修改:

strlen is defined in in <cstring> in c++. strlen在c ++中的<cstring>中定义。 The modifications that follow do not mention strlen . 随后的修改没有提到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. 由此,我可以得出结论,C ++中的签名必须与C中的签名完全相同,并且由于C没有constexpr ,因此在技术上不兼容。 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. 就是说,这是不合规的事情之一,除了在一个平台上依赖它,然后在另一个平台上找不到它之外,不太可能造成任何伤害。

Firstly, you might be confusing the functionality of the two: strlen() gives the length of the whole string and sizeof() gives the size of memory space occupied by the data type in memory. 首先,您可能会混淆两者的功能:strlen()给出整个字符串的长度,sizeof()给出内存中数据类型占用的内存空间大小。

The function sizeof() is a compile-time expression because the memory to your variable is allocated during compile-time(given its not dynamically written). 函数sizeof()是一个编译时表达式,因为变量的内存是在编译时分配的(因为它不是动态写入的)。 Thus, giving you the size of memory occupied by the data type. 因此,为您提供了数据类型所占用的内存大小。 It doesn't care about the value of the variable, just cares about memory space. 它不在乎变量的值,只在乎内存空间。

Whereas, strlen() is a function that takes a pointer to a character, and keeps incrementing the memory from this character o, looking for a NULL character, which is at the end of the string. strlen()是一个函数,它使用一个指向字符的指针,并从该字符o开始不断增加内存,以寻找字符串末尾的NULL字符。 It counts the number of characters before it finds the NULL character.Basically, giving you the length. 它会在找到NULL字符之前先对字符数进行计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM