[libcxx-dev] charconv.to.chars/integral.bool.fail.cpp is incorrect

Stephan T. Lavavej via libcxx-dev libcxx-dev at lists.llvm.org
Thu Jul 18 21:50:19 PDT 2019


test/std/utilities/charconv/charconv.to.chars/integral.bool.fail.cpp claims that "Integral cannot be bool." and expects that calling to_chars() with a bool should fail to compile. This is an incorrect interpretation of the Standardese. MSVC's STL implements the overload set exactly as depicted in the Standard, so this test case compiles successfully.

The Standardese is N4820 20.19.2 [charconv.to.chars]/7 "Remarks: The implementation shall provide overloads for all signed and unsigned integer types and char as the type of the parameter value." Observe that these are depicted as overloads - not as a template.

If implemented as overloads, here's what happens:

C:\Temp>type woof.cpp
#include <stdio.h>

void ToChars(char) { puts("char"); }
void ToChars(signed char) { puts("signed char"); }
void ToChars(unsigned char) { puts("unsigned char"); }
void ToChars(short) { puts("short"); }
void ToChars(unsigned short) { puts("unsigned short"); }
void ToChars(int) { puts("int"); }
void ToChars(unsigned int) { puts("unsigned int"); }
void ToChars(long) { puts("long"); }
void ToChars(unsigned long) { puts("unsigned long"); }
void ToChars(long long) { puts("long long"); }
void ToChars(unsigned long long) { puts("unsigned long long"); }
void ToChars(float) { puts("float"); }
void ToChars(double) { puts("double"); }
void ToChars(long double) { puts("long double"); }

int main() {

C:\Temp>cl /EHsc /nologo /W4 /std:c++17 /permissive- woof.cpp && woof

C:\Temp>clang-cl -m64 /EHsc /nologo /W4 /std:c++17 /permissive- -fno-ms-compatibility woof.cpp && woof

C1XX and Clang agree: the int overload is called. This is because bool undergoes a Promotion to int, and that's an exact match for int, while all other types require a Conversion from int to that type.

This test case should be reworked. I mention it, instead of simply fixing the test case, because it indicates that libcxx's implementation of to_chars() for integral types is also incorrect.


More information about the libcxx-dev mailing list