[libcxx-commits] [PATCH] D116487: [libc++][NFC] Introduce __fits_in_sso()

Nikolas Klauser via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 3 13:11:03 PST 2022


philnik added a comment.

In D116487#3218176 <https://reviews.llvm.org/D116487#3218176>, @ldionne wrote:

> Are there any tests that need to be modified? I would assume that tests that check for reallocation when a string is assigned-to (or similar stuff) will need to be conditionalized on whether the test is running inside a constexpr context too, right?
>
> Finally, and most importantly -- is this implementation strategy really valid? I had thought about this when I originally wrote the constexpr string paper, and IIRC one of the issues was this:
>
>   constexpr std::string s = "short"; // Anything short enough to normally use the SSO. Since this is inside constexpr, though, SSO isn't used.
>   
>   void use(std::string const* str) {
>     // ...
>   }
>   
>   int main() { use(s); }
>
> The problem here is that when `s` is constructed, it is a constexpr context so we don't use the SSO. When we actually *use* `s`, though, it's not a constexpr context so if we check `s`'s size and ask whether it is using SSO, we'll believe that yes, we are using it (because it would normally fit in the small buffer).
>
> Is this not an issue somehow? It's been a while since I've thought about this.

You have to deallocate anything that you allocated during constant evaluation. So using a constexpr string in a non-constexpr context is illegal IIUC. https://godbolt.org/z/KKv3YnP4a


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116487/new/

https://reviews.llvm.org/D116487



More information about the libcxx-commits mailing list