[libcxx-commits] [libcxx] [libc++] Optimize num_put integral functions (PR #120859)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jan 15 06:39:32 PST 2025


================
@@ -12,40 +12,35 @@
 
 // iter_type put(iter_type s, ios_base& iob, char_type fill, void* v) const;
 
-#include <locale>
-#include <ios>
 #include <cassert>
-#include <streambuf>
-#include "test_macros.h"
+#include <ios>
+#include <locale>
+
 #include "test_iterators.h"
 
 typedef std::num_put<char, cpp17_output_iterator<char*> > F;
 
-class my_facet
-    : public F
-{
+class my_facet : public F {
 public:
-    explicit my_facet(std::size_t refs = 0)
-        : F(refs) {}
+  explicit my_facet(std::size_t refs = 0) : F(refs) {}
 };
 
-int main(int, char**)
-{
-    const my_facet f(1);
-    {
-        std::ios ios(0);
-        void* v = 0;
-        char str[50];
-        cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
-        std::string ex(str, base(iter));
-        char expected_str[32] = {};
-        // num_put::put uses %p for pointer types, but the exact format of %p is
-        // implementation defined behavior for the C library. Compare output to
-        // snprintf for portability.
-        int rc = snprintf(expected_str, sizeof(expected_str), "%p", v);
-        assert(rc > 0);
-        assert(ex == expected_str);
-    }
+int main(int, char**) {
+  const my_facet f(1);
+  {
+    std::ios ios(nullptr);
+    void* v = nullptr;
+    char str[50];
+    cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
+    std::string ex(str, base(iter));
+    char expected_str[32] = {};
+    // num_put::put uses %p for pointer types, but the exact format of %p is
+    // implementation defined behavior for the C library. Compare output to
+    // snprintf for portability.
+    int rc = snprintf(expected_str, sizeof(expected_str), "%p", v);
+    assert(rc > 0);
+    assert(ex == expected_str || ex == "0x0");
----------------
ldionne wrote:

```suggestion
    // num_put::put uses %p for pointer types, but the exact format of %p
    // is implementation-defined.
    assert(!ex.empty()); // it seems reasonable to at least expect a non-empty result from all implementations
    LIBCPP_ASSERT(ex == "0x0");
```

Can you write a LWG issue for this? To clarify whether our desired behaviour's and libstdc++'s current behaviour is conforming.

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


More information about the libcxx-commits mailing list