vendredi 15 janvier 2021

C++ SSO : How to programatically find if a std::wstring is allocated with Short String Optimization?

How to programatically find if a std::wstring is allocated with Short String Optimization? I was trying to detect such cases and use reserve to move out of the SSO.

The below code prints a small difference in the addresses:

#include <string>
#include <iostream>
int main () 
{
    std::wstring str = L"H";
    std::cout<<&str<<" "<<(void*)str.data()<<"\n";
} 

Output example:

0x7ffc39465220 0x7ffc39465230

enter image description here

Aucun commentaire:

Enregistrer un commentaire