How to make const a rtl::Static

Stephan Bergmann sbergman at redhat.com
Tue Oct 17 15:54:45 UTC 2017


On 10/15/2017 03:28 PM, Laurent BP wrote:
> I would like to make const a rtl::Static. I tryed:
> struct MyConstStatic : public rtl::Static<const MyType, MyConstStatic> {};

First of all, internal LO code no longer needs to use rtl::Static, as 
all relevant toolchains support C++11 "magic statics" by now.

> But now, how to initialize it? How when it is an array like NfKeywordTable
> https://opengrok.libreoffice.org/xref/core/include/svl/nfkeytab.hxx#107

If you need something other than a default-initialized T, there's 
rtl::StaticWithInit.  With your specific example, that would be 
something like

> struct MyConstStaticInit {
>     NfKeywordTable operator ()() {
>         NfKeywordTable t;
>         t[NF_KEY_E] = "...";
>         t[NF_KEY_AMPM] = "...";
>         // ...
>         t[NF_KEY_THAI_T] = "...";
>         return t;
>     }
> };
> struct MyConstStatic:
>     public rtl::StaticWithInit<NfKeywordTable const, MyConstStaticInit>
> {};
> OUString test(NfKeywordIndex n) { return MyConstStatic::get()[n]; }

(But of course a class that, unlike the existing NfKeywordTable, can be 
initialized with a std::initializer_list would be more useful in such a 
case.  Together with the non-necessity to use rtl::Static in the first 
place, all could boil down to something like

> OUString test(NfKeywordIndex n) {
>     static NfKeywordTable const t{"...", "...", /*...*/ "..."};
>     return t[n];
> };

then.)


More information about the LibreOffice mailing list