How to use string constants with std::map

Stephan Bergmann sbergman at redhat.com
Wed Oct 11 18:14:14 UTC 2023


On 10/11/23 19:24, Regina Henschel wrote:
> How to write the map? In the past it would have been something like the 
> following:
> 
> static const std::map<OUString, std::vector<OUString>> 
> aConnectionSiteAngleMap{
>      {"accentBorderCallout1",{"0","cd4","cd2","3cd4"}},
> ...
>      {"wedgeRoundRectCallout",{"3cd4","cd2","cd4","0","cd4"}}
> };
> 
> All strings are constants and neither the map nor the strings will ever 
> change.
> 
> How do I have to write it now?

You can keep writing it as above, or your could write it as

> static const std::map<OUString, std::vector<OUString>>
> aConnectionSiteAngleMap{
>      {u"accentBorderCallout1"_ustr,{u"0"_ustr,u"cd4"_ustr,u"cd2"_ustr,u"3cd4"_ustr}},
> ...
>      {u"wedgeRoundRectCallout"_ustr,{u"3cd4"_ustr,u"cd2"_ustr,u"cd4"_ustr,u"0"_ustr,u"cd4"_ustr}}
> };

which is a bit more efficient as it doesn't need to construct the 
OUString instances it runtime (but it still needs to construct the 
std::vector and std::map instances at runtime).

(You can't write it as

> static constexpr std::map<OUString, std::vector<OUString>>
> aConnectionSiteAngleMap{
>      {u"accentBorderCallout1"_ustr,{u"0"_ustr,u"cd4"_ustr,u"cd2"_ustr,u"3cd4"_ustr}},
> ...
>      {u"wedgeRoundRectCallout"_ustr,{u"3cd4"_ustr,u"cd2"_ustr,u"cd4"_ustr,u"0"_ustr,u"cd4"_ustr}}
> };

as std::map is not constexpr-ready in C++20, and neither is std::vector 
in some standard library implementations, like in debug-mode libstdc++.)



More information about the LibreOffice mailing list