Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

If my code has this constexpr string

constexpr char my_str[] = "hello";

the type of my_str contains information about its size, i.e. sizeof(my_str) is a constant 6, and can be used anywhere a constant is required.

What about strlen(my_str)? Can/should it also be evaluated to a compile-time constant?

Here is an example for yes: https://ideone.com/2U65bN

Here is an example for no: http://coliru.stacked-crooked.com/a/8cb094776dfc5969

What does the Standard say about this? Surely not "maybe"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

In C++17 you can use std::char_traits::length, which is constexpr, instead of strlen.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...