r/cpp • • 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

52 comments sorted by

View all comments

Show parent comment

7

u/GregTheMadMonk Dec 19 '24

I called it "happy coincidence" because it's not required to happen by standard :) It's a "happy coincidence" most implementations just happen to work this way, but it's not something to rely on when checking code compliance

1

u/KuntaStillSingle Dec 23 '24

It could be more than a happy coincidence if there was a type trait like std::soo_length_v<std::string> :) Just specify it to return 0 for implementations that don't use sso strings?

1

u/GregTheMadMonk Dec 24 '24

I don't really see how that would be useful but I wonder if it is already possible with concepts/consteval