[Development] Oslo, we have a problem</apollo 13> [char8_t]

Fabian Kosmale fabian.kosmale at qt.io
Sat Jul 6 14:50:05 CEST 2019


Hi,

Actually, most of the time there is actually an option 3. Add yet another overload, this time for int. This will match the 0 literal without any conversion, and we can then just call the nullptr overload inside. Unfortunately, C++20 still does not have constexpr parameters, else we could ensure at compile time that only 0 is allowed. Currently, we can only add a runtime assert as far as I can see.

The described technique  does of course only work when there is no need for a useful int overload, but at least for QByteArray that is not an issue as far as I can see. Also, it might be a good idea to delete the char overload, else  calling QByteArray::prepend with a character literal will "work" but it would have unexpected behaviour.

See https://godbolt.org/z/e6OinY for how this would look for a trivial function.
[https://github.com/mattgodbolt/compiler-explorer-image/blob/master/logo/favicon.png?raw=true]<https://godbolt.org/z/e6OinY>
Compiler Explorer - C++<https://godbolt.org/z/e6OinY>
auto test(const char* c) -> int { return 0; } auto test(const char8_t* u) -> int { return 1; } static_assert(std::is_same_v<decltype(0), int>); [[deprecated("This overload is only meant to cover nullpointers passed as 0/NULL, please use nullptr instead")]] int test(int n) { assert(n == 0); return 2; } int test(std::nullptr_t) {return 3;} int test(char) = delete; // not required, but might be a good idea int main() { // test('c'); deleted assert( test(nullptr) + test("Hallo!") + test(u8"Ça va?") + test(0) == 6); return 0; }
godbolt.org


Fabian

--
Fabian Kosmale
Software Engineer

The Qt Company GmbH
Erich-Thilo-Str. 10
D-12489 Berlin
fabian.kosmale at qt.io
+49 1638686070
http://qt.io

Geschäftsführer: Mika Pälsi,
Juha Varelius, Mika Harjuaho
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht
Charlottenburg, HRB 144331 B
--
________________________________
Von: Development <development-bounces at qt-project.org> im Auftrag von Mutz, Marc via Development <development at qt-project.org>
Gesendet: Samstag, 6. Juli 2019 12:43
An: development at qt-project.org
Betreff: [Development] Oslo, we have a problem</apollo 13> [char8_t]

Hi,

C++20 is coming along, and it brings a disruptive change, one that far
surpasses the C++17 noexcept break: u8"Hello" is now const char8_t[], no
longer const char[].

To estimate the amount of breakage this will cause, assuming that using
u8"" is good practice today, to indicate that a string is in UTF-8. I've
tried to have at least QByteArray not break... and failed.

The initial idea is simple enough: add const char8_t* overloads for
const char* functions. This breaks passing nullptr, so you also add
std::nullptr_t overloads. This, however, still doesn't fix the case
where a 0 is passed. I've expected that the std::nullptr_t overload is a
preferred match over the const char[8_t]* ones, but GCC 9.1 disagrees,
and tells me it's still ambiguous.

So, if GCC is right, we have no way of adapting our API to not break in
C++20. So we need to decide what to break:

a) using 0 for nullptr, or
b) using u8"Hello" at all

The forward-looking choice would be to break (a) and support (b).

Opinions?

Thanks,
Marc
_______________________________________________
Development mailing list
Development at qt-project.org
https://lists.qt-project.org/listinfo/development
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20190706/878fde8d/attachment.html>


More information about the Development mailing list