r/cpp • u/SPEKTRUMdagreat • Jun 25 '26
std::formatter specialization for smart pointers
Currently something like
std::unique_ptr<int> u_ptr = std::make_unique<int>(42);
std::shared_ptr<int> s_ptr = std::make_shared<int>(42);
std::println("uptr: {}", u_ptr);
std::println("sptr: {}", s_ptr);is ill formed because there is no specialization of std::formatter for smart pointer types.
On the other hand,
std::cout << "uptr: " << u_ptr << '\n';
std::cout << "sptr: " << s_ptr << '\n';do work because ostream defines overloads for smart pointer types.
Please let me know if anyone has thoughts or if there's some reason that these types haven't been specialized that I'm missing.
16
Upvotes
View all comments
Show parent comment
2
u/aruisdante Jun 29 '26
I dunno man, I’d expect the format of a pointer to, you know, printer the pointer’s value, not the value of the thing the pointer pointed to. Since if the printer decides to do that, there’s no way for me to print the pointer’s value and see visually that two pointers point to the same object and not two different objects that happen to have the same value.
Since clearly my expectation of what printing a pointer should do is different than yours, it makes sense that they decided “let the project decide for themselves since it’s easy enough to write.”