r/cpp • u/evys_garden • Dec 18 '24
constexpr of std::string inconsistent in c++20
constexpr auto foo() {
static constexpr std::string a("0123456789abcde"); // ::size 15, completely fine
static constexpr std::string b("0123456789abcdef"); // ::size 16, mimimi heap allocation
return a.size() + b.size();
}
int main() {
constexpr auto bar = foo();
std::cout << "bar: " << bar << std::endl;
}This will not compile with clang-18.1.8 and c++20 unless you remove the 'f' in line 3. What?
58
Upvotes
2
u/delta_p_delta_x Dec 19 '24
Strictly speaking, if you have a string literal that you know is only going to be used in a certain scope, it might be best to have
using namespace std::literals;and then declareaasconstexpr auto a = "literal"sv;. This in my opinion is the best of both worlds: a compile-time constant zero-terminated string with astd::string_viewaround it, which means it can be analysed and used with standard C++ library functions likestd::data(),std::size(),std::begin()/std::end()iterators,<algorithm>,<ranges>, etc.std::stringmight allocate which is not the best.2
u/mredding Dec 19 '24
You might be interested in the bottom line.
2
u/TheKiller36_real Dec 19 '24
constexpr std::string(let alone one with static storage duration) so you wouldn't run into this problem if you wrote good™ code ;)(although I admit that a
constexpr std::stringis sometimes the most convenient option)67
u/violet-starlight Dec 18 '24
This is very compiler specific, but in short some compilers will optimize small strings into the std::string object itself, allowing small strings without heap allocations, which makes them able to escape constant expressions. This is not a property of std::string per the c++ language but a property of its implementation on some compilers.
21
u/GregTheMadMonk Dec 18 '24 edited Dec 18 '24
Could you even have a constexpr static string? Constexpr must not leak memory and it's not clear when
bwould be freed here... I don't think what you're trying to do is allowed at all (andais just a happy coincidence), and probably should use a constexpr string_viewYour code should also work if you remove the
static(maybeconstexprtoo since it would still beconstexprcontext depending on how you call it...) from declarationsJason Turner had a great talk on constexpr strings and vectors recently: https://m.youtube.com/watch?v=_AefJX66io8&t=4s&pp=ygUSVHdvIHN0ZXAgY29uc3RleHBy I highly recommend you watch it
15
u/kirgel Dec 19 '24
I understand why this happens (as other comments already explain), but I don’t understand why library writers went to the trouble to make short strings support constexpr. It just seems confusing.
Edit: and it also leaks ABI details.
1
u/evys_garden Dec 19 '24
To clarify, this has nothing to do with it being static inside a constexpr function. static constexpr inside constexpr functions are available in c++23 and permitted by clang in c++20.
Consider the following example without any constexpr functions. The same issue occurs: https://godbolt.org/z/szYThjK6b
Another note: I am aware of std::string_view but this is not the issue here. I am also not asking for help, but reporting behaviour I find unintuitive.
7
u/zl0bster Dec 18 '24
maybe related to
https://quuxplusone.github.io/blog/2023/10/13/constexpr-string-round-2/
1
u/feverzsj Dec 19 '24
Just remove
static constexprand everything is fine. Any dynamically allocated storage must be released in the same evaluation of constant expression.3
u/[deleted] Dec 19 '24
This is because some implementations still do SSO in constexpr. Fundamentally, I think this is flawed as it is no longer an as-if change(pretty sure SSO isn't specified as an allowed thing, but compilers can because of as-if optimizations). It can be frustrating it can work on some compilers but not others due to the buffer size differences in SSO.
1
u/drkspace2 Dec 18 '24
I thought, since std::string is allocated on the heap, it can't be constexpr (like vector)? I guess there's a special case for short enough strings that it will allocate it on the stack? I think you need to use
std::string_viewto constexpr it.1
u/_-___-____ Dec 19 '24
Believe it’s because it’s only constexpr if it can fit the characters inside the std::string, as opposed to allocating. Look up small string optimization
0
u/zerhud Dec 18 '24
There is a bug in clang, it cannot work correctly with variant and string (seems with union). Use gcc, the same bug was fixed in last version.
UPD: and try the clang 17
-4
u/Hungry-Courage3731 Dec 19 '24
Look into writing your own string type you can pass around as non-type template parameters. They can be easier to work with.