From: Jean-Marc Lasgouttes Date: Tue, 12 Mar 2019 11:40:32 +0000 (+0100) Subject: Make TextMetrics noncopyable X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=6d67dfa2b42845206d8b40e9416f047e372cfa23;p=features.git Make TextMetrics noncopyable This is done by declaring unimplemented private copy constructor and assignment operator. This breaks compilation in BufferView::textMetrics, which does a copy when inserting a TextMetrics object in the cache. Some C++11 wizardry I will not pretend to completely understand saves the day. See the following page for details: https://en.cppreference.com/w/cpp/container/map/emplace This avoids real world bugs like #11512. --- diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 50df1aade2..900207c238 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -2542,8 +2542,9 @@ TextMetrics & BufferView::textMetrics(Text const * t) LBUFERR(t); TextMetricsCache::iterator tmc_it = d->text_metrics_.find(t); if (tmc_it == d->text_metrics_.end()) { - tmc_it = d->text_metrics_.insert( - make_pair(t, TextMetrics(this, const_cast(t)))).first; + tmc_it = d->text_metrics_.emplace(std::piecewise_construct, + std::forward_as_tuple(t), + std::forward_as_tuple(this, const_cast(t))).first; } return tmc_it->second; } diff --git a/src/TextMetrics.h b/src/TextMetrics.h index 26156fa100..006836fb0a 100644 --- a/src/TextMetrics.h +++ b/src/TextMetrics.h @@ -33,6 +33,9 @@ class Text; /// A map from a Text to the map of paragraphs metrics class TextMetrics { + /// noncopyable + TextMetrics(TextMetrics const &); + void operator=(TextMetrics const &); public: /// Default constructor (only here for STL containers). TextMetrics() : bv_(0), text_(0), max_width_(0) {}