constinit, here we come

Desktop / LibreOffice - Stephan Bergmann [redhat.com] - 25 September 2019 18:55 EDT

Following up on b13421011d9377676e1adc282634991d5064a866 "better data structures for some static const vars":

- Make the o3tl::sorted_vector ctor taking an initializer_list constexpr. "Add Constexpr Modifiers to Functions in and Headers", which will be in C++2a and is already implemented by recent libc++ and libstdc++ according to , makes std::sort constexpr but explicitly keeps std::stable_sort non-constexpr. ("Algorithms stable_partition, inplace_merge and stable_sort allocate memory, construct variables using placement new, use unique_ptr and do other things not acceptable in constexpr expressions. Making those algorithms constexpr seems to be a hard task that would require a lot of intrinsics. Those algorithms are not marked with constexpr in this wording.") But keep o3tl::sorted_vector::Resort (which was introduced in c59355e936446fe55960209e543b072acb6b2170 "fdo#58793: re-implement SwpHintsArray::Resort()") using std::stable_sort instead of std::sort, in case that is relevant for its pre-exisiting uses.

- "Making std::vector constexpr", which was voted into C++2a according to "Editors' Report --Programming Languages -- C++", will make the relevant parts of std::vector constexpr. But none of libc++, libstdc++, and MSVC implement that as of now.

- Introduce HAVE_CPP_CONSTINIT_SORTED_VECTOR to hide the constinit behind for now for the one case from b13421011d9377676e1adc282634991d5064a866 "better data structures for some static const vars" that can clearly be constinit once constexpr std::vector is supported by compilers. The other three cases (s_aContainerDocumentCommands in chart2/source/controller/main/CommandDispatchContainer.cxx, aMetricCompatibleMap in vcl/source/font/PhysicalFontCollection.cxx, and aBlacklist in writerfilter/source/dmapper/PropertyMap.cxx) would each need a constexpr OUString first. (Technically, the constinit would not even be needed, but it nicely documents our intent that this will actually be initialized at compile-time once compilers support that.)

Change-Id: Ibeb138f120528be3a7a09b3912143bf795fbab29 Reviewed-on: https://gerrit.libreoffice.org/79556

689dc632875c constinit, here we come
config_host/config_global.h.in | 5 +++++
configure.ac | 22 ++++++++++++++++++++++
include/o3tl/sorted_vector.hxx | 4 ++--
sc/source/core/tool/token.cxx | 3 +++
4 files changed, 32 insertions(+), 2 deletions(-)

Upstream: cgit.freedesktop.org


  • Share