summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2022-11-08 11:51:13 +0100
committerStephan Bergmann <sbergman@redhat.com>2022-11-08 17:28:32 +0100
commit46875d83476942ca215429c837a3457f55c3ccb0 (patch)
tree67724f25960c4582baefa9042b8d376115d19f30
parent5d26b851f27a55cd8cb34973b0ad8d7a3801af7e (diff)
A better fix for C++23 P2266R1
After 6d6a143913603b040c10a5db83c2103557899011 "Address some of the sprintf in vcl/source/fontsubset/cff.cxx", --with-latest-c++ builds that pick up a C++23 compiler that implements <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2266r1.html> "P2266R1: Simpler implicit move" started to fail with something like > vcl/source/fontsubset/cff.cxx:2061:16: error: no viable conversion from returned value of type 'char[64]' to function return type 'OString' > return aDefaultGlyphName; > ^~~~~~~~~~~~~~~~~ [...] > include/rtl/string.hxx:313:5: note: candidate constructor [with T = char[64]] not viable: expects an lvalue for 1st argument > OString( T& value, typename libreoffice_internal::NonConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() ) > ^ etc. So I figured there should be something better than 433ab39b2175bdadb4916373cd2dc8e1aabc08a5 "Adapt implicit OString return value construction to C++23 P2266R1" (which this commit reverts, modulo its conflicts in comphelper/source/xml/xmltools.cxx and sc/source/filter/xcl97/XclExpChangeTrack.cxx) to address the underlying issue in a way that keeps code that works up to C++20 also working in C++23. (The fix is only relevant for non-explicit constructors that involve NonConstCharArrayDetector and non-const lvalue references, not for other functions involving those. OUString has a similar constructor but which is explicit, and OUStringBuffer doesn't have any similar constructors at all, so this only affects OString and OStringBuffer constructors.) Change-Id: I31cf16b9507899f5999243f8467dfa24bc94c5ac Reviewed-on: https://gerrit.libreoffice.org/c/core/+/142455 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
-rw-r--r--include/rtl/strbuf.hxx11
-rw-r--r--include/rtl/string.hxx9
-rw-r--r--l10ntools/source/po.cxx4
-rw-r--r--sal/osl/unx/uunxapi.cxx2
-rw-r--r--sal/qa/rtl/strings/nonconstarray.cxx20
-rw-r--r--sc/source/filter/excel/xestream.cxx2
-rw-r--r--sc/source/filter/html/htmlexp.cxx2
-rw-r--r--sc/source/filter/xcl97/XclExpChangeTrack.cxx2
-rw-r--r--unotools/source/i18n/resmgr.cxx2
9 files changed, 47 insertions, 7 deletions
diff --git a/include/rtl/strbuf.hxx b/include/rtl/strbuf.hxx
index dbdcd7fea39a..97368ce3ca4d 100644
--- a/include/rtl/strbuf.hxx
+++ b/include/rtl/strbuf.hxx
@@ -180,6 +180,17 @@ public:
rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
}
+#if __cplusplus > 202002L // C++23 P2266R3 "Simpler implicit move"
+ template< typename T >
+ OStringBuffer( T&& value, typename libreoffice_internal::NonConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy())
+ : pData(NULL)
+ {
+ sal_Int32 length = rtl_str_getLength( value );
+ nCapacity = length + 16;
+ rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
+ }
+#endif
+
/**
Constructs a string buffer so that it represents the same
sequence of characters as the string literal.
diff --git a/include/rtl/string.hxx b/include/rtl/string.hxx
index 472d3074bd1b..24c6a64008cf 100644
--- a/include/rtl/string.hxx
+++ b/include/rtl/string.hxx
@@ -316,6 +316,15 @@ public:
rtl_string_newFromStr( &pData, value );
}
+#if __cplusplus > 202002L // C++23 P2266R3 "Simpler implicit move"
+ template< typename T >
+ OString( T&& value, typename libreoffice_internal::NonConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
+ {
+ pData = NULL;
+ rtl_string_newFromStr( &pData, value );
+ }
+#endif
+
/**
New string from a string literal.
diff --git a/l10ntools/source/po.cxx b/l10ntools/source/po.cxx
index b053067ea163..e075d52b0bd5 100644
--- a/l10ntools/source/po.cxx
+++ b/l10ntools/source/po.cxx
@@ -425,7 +425,7 @@ OString PoEntry::genKeyId(const OString& rGenerator)
nCRC >>= 6;
}
sKeyId[5] = '\0';
- return OString(sKeyId);
+ return sKeyId;
}
namespace
@@ -437,7 +437,7 @@ namespace
struct tm* pNow = localtime(&aNow);
char pBuff[50];
strftime( pBuff, sizeof pBuff, "%Y-%m-%d %H:%M%z", pNow );
- return OString(pBuff);
+ return pBuff;
}
}
diff --git a/sal/osl/unx/uunxapi.cxx b/sal/osl/unx/uunxapi.cxx
index 5311bc71917f..d1fc6c4f9d3f 100644
--- a/sal/osl/unx/uunxapi.cxx
+++ b/sal/osl/unx/uunxapi.cxx
@@ -154,7 +154,7 @@ static OString macxp_resolveAliasAndConvert(OString const & p)
{
strcpy(path, p.getStr());
macxp_resolveAlias(path, PATH_MAX);
- return OString(path);
+ return path;
}
return p;
}
diff --git a/sal/qa/rtl/strings/nonconstarray.cxx b/sal/qa/rtl/strings/nonconstarray.cxx
index 4b66e4e311c3..d759049582d0 100644
--- a/sal/qa/rtl/strings/nonconstarray.cxx
+++ b/sal/qa/rtl/strings/nonconstarray.cxx
@@ -12,11 +12,24 @@
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
+#include <rtl/strbuf.hxx>
#include <rtl/string.hxx>
#include <rtl/ustring.hxx>
namespace
{
+OString returnOString()
+{
+ char buf[] = "foo\0bar";
+ return buf;
+}
+
+OStringBuffer returnOStringBuffer()
+{
+ char buf[] = "foo\0bar";
+ return buf;
+}
+
class Test : public CppUnit::TestFixture
{
private:
@@ -81,10 +94,17 @@ private:
}
}
+ void testP2266R3()
+ {
+ CPPUNIT_ASSERT_EQUAL(OString("foo"), returnOString());
+ CPPUNIT_ASSERT_EQUAL(OString("foo"), returnOStringBuffer().makeStringAndClear());
+ }
+
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testOString);
CPPUNIT_TEST(testOUStringChar);
CPPUNIT_TEST(testOUStringChar16t);
+ CPPUNIT_TEST(testP2266R3);
CPPUNIT_TEST_SUITE_END();
};
diff --git a/sc/source/filter/excel/xestream.cxx b/sc/source/filter/excel/xestream.cxx
index ee75b188b0f7..0ee697dfeba8 100644
--- a/sc/source/filter/excel/xestream.cxx
+++ b/sc/source/filter/excel/xestream.cxx
@@ -712,7 +712,7 @@ OString XclXmlUtils::ToOString( const Color& rColor )
char buf[9];
o3tl::sprintf( buf, "%.2X%.2X%.2X%.2X", rColor.GetAlpha(), rColor.GetRed(), rColor.GetGreen(), rColor.GetBlue() );
buf[8] = '\0';
- return OString(buf);
+ return buf;
}
OStringBuffer& XclXmlUtils::ToOString( OStringBuffer& s, const ScAddress& rAddress )
diff --git a/sc/source/filter/html/htmlexp.cxx b/sc/source/filter/html/htmlexp.cxx
index 4cc01d868603..16db15316121 100644
--- a/sc/source/filter/html/htmlexp.cxx
+++ b/sc/source/filter/html/htmlexp.cxx
@@ -191,7 +191,7 @@ static OString lcl_makeHTMLColorTriplet(const Color& rColor)
// <font COLOR="#00FF40">hello</font>
snprintf( buf, 24, "\"#%02X%02X%02X\"", rColor.GetRed(), rColor.GetGreen(), rColor.GetBlue() );
- return OString(buf);
+ return buf;
}
ScHTMLExport::ScHTMLExport( SvStream& rStrmP, OUString _aBaseURL, ScDocument* pDocP,
diff --git a/sc/source/filter/xcl97/XclExpChangeTrack.cxx b/sc/source/filter/xcl97/XclExpChangeTrack.cxx
index bbee834806c3..57fd143bf659 100644
--- a/sc/source/filter/xcl97/XclExpChangeTrack.cxx
+++ b/sc/source/filter/xcl97/XclExpChangeTrack.cxx
@@ -48,7 +48,7 @@ static OString lcl_DateTimeToOString( const DateTime& rDateTime )
rDateTime.GetYear(), rDateTime.GetMonth(), rDateTime.GetDay(),
rDateTime.GetHour(), rDateTime.GetMin(), rDateTime.GetSec(),
rDateTime.GetNanoSec() );
- return OString(sBuf);
+ return sBuf;
}
// local functions
diff --git a/unotools/source/i18n/resmgr.cxx b/unotools/source/i18n/resmgr.cxx
index 641fa61be6dc..ddedf5b9e8a2 100644
--- a/unotools/source/i18n/resmgr.cxx
+++ b/unotools/source/i18n/resmgr.cxx
@@ -92,7 +92,7 @@ namespace
nCRC >>= 6;
}
sKeyId[5] = '\0';
- return OString(sKeyId);
+ return sKeyId;
}
}