[libcxx-commits] [libcxx] [libc++] Switch to a new technique for std::to_chars availability (PR #234468)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 22 02:22:23 PDT 2026


================
@@ -19,39 +19,86 @@
 #endif
 
 _LIBCPP_BEGIN_NAMESPACE_STD
-_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
 #if _LIBCPP_STD_VER >= 17
 
-_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_EXPORTED_FROM_ABI to_chars_result
-to_chars(char* __first, char* __last, float __value);
+#  if _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT
+
+_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
-_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_EXPORTED_FROM_ABI to_chars_result
-to_chars(char* __first, char* __last, double __value);
+_LIBCPP_EXPORTED_FROM_ABI to_chars_result to_chars(char* __first, char* __last, float __value);
 
-_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_EXPORTED_FROM_ABI to_chars_result
-to_chars(char* __first, char* __last, long double __value);
+_LIBCPP_EXPORTED_FROM_ABI to_chars_result to_chars(char* __first, char* __last, double __value);
 
-_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_EXPORTED_FROM_ABI to_chars_result
-to_chars(char* __first, char* __last, float __value, chars_format __fmt);
+_LIBCPP_EXPORTED_FROM_ABI to_chars_result to_chars(char* __first, char* __last, long double __value);
 
-_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_EXPORTED_FROM_ABI to_chars_result
-to_chars(char* __first, char* __last, double __value, chars_format __fmt);
+_LIBCPP_EXPORTED_FROM_ABI to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt);
 
-_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_EXPORTED_FROM_ABI to_chars_result
+_LIBCPP_EXPORTED_FROM_ABI to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt);
+
+_LIBCPP_EXPORTED_FROM_ABI to_chars_result
 to_chars(char* __first, char* __last, long double __value, chars_format __fmt);
 
-_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_EXPORTED_FROM_ABI to_chars_result
+_LIBCPP_EXPORTED_FROM_ABI to_chars_result
 to_chars(char* __first, char* __last, float __value, chars_format __fmt, int __precision);
 
-_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_EXPORTED_FROM_ABI to_chars_result
+_LIBCPP_EXPORTED_FROM_ABI to_chars_result
 to_chars(char* __first, char* __last, double __value, chars_format __fmt, int __precision);
 
-_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_EXPORTED_FROM_ABI to_chars_result
+_LIBCPP_EXPORTED_FROM_ABI to_chars_result
 to_chars(char* __first, char* __last, long double __value, chars_format __fmt, int __precision);
-#endif // _LIBCPP_STD_VER >= 17
 
 _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
+
+#  else
+
+// When the floating-point overloads of std::to_chars are not provided by the built library on the deployment
+// target, we define them as inline functions that forward to a function that doesn't exist anywhere. Using
+// them then results in a link-time error that mentions the cause of the problem. This is used instead of
+// availability attributes because Clang diagnoses them too eagerly (http://llvm.org/PR61563). With this approach,
+// there is only an error when the code is actually emitted.
+to_chars_result __to_chars_floating_point_is_not_available_on_this_deployment_target();
----------------
philnik777 wrote:

Can we use `[[gnu::error]]` to improve fidelity a bit?

https://github.com/llvm/llvm-project/pull/234468


More information about the libcxx-commits mailing list