Page MenuHomePhabricator

[libcxx] LWG2221: No formatted output operator for nullptr

Authored by K-ballo on Jun 1 2017, 6:22 AM.



With effects equivalent to os << (const void*)nullptr.

Diff Detail

Event Timeline

K-ballo created this revision.Jun 1 2017, 6:22 AM
lichray added inline comments.

Oh, common, I persuaded the committee to allow you to print a (null) and you don't do it...

Other than the actual text being output, this LGTM.
I'ld like to see the changes I suggested in the test go in, but they're really minor.


I think that (null) is a better thing to output here than 0x0.


You could just say `assert(!sb.str().empty()) here; no need to save the string in a variable.


Might as well check for a non-empty string here, too.

Quuxplusone added inline comments.

Are you two implying that

*this << (const void *)0;

does *not* print (null)? It certainly should, IMO. (I mean, it'll call num_put for void* in the current locale, but I would naturally expect that to print (null).)
Anyway, whether the current locale prints null as (null) or not, there is great potential utility in using the same format for all pointers, as K-ballo is doing here. I'd really like to be able to

std::ostringstream oss;
oss << nullptr;  // equivalently: oss << (void*)0;
void *p;
std::istringstream(oss.str()) >> p;  // should read in a null pointer, not derp out

FWIW, it looks like libc++ currently *does* print null pointers as (nil), which is not quite the same thing as (null). libstdc++ prints them as 0.

K-ballo added inline comments.Dec 13 2017, 12:06 PM

It's been a while now, but I seem to recall that the reason we settled on (*this) << (const void*)0 was precisely so that it would go through num_put, as the rest of the pointers do. As I read back on the specification, however, I am no longer sure that such an implementation is conforming. Maybe if we were to call the facet directly...

I am no longer interested in this issue. If anyone wants to take over control I would be happy to yield it; otherwise, I'll implement whatever I'm instructed to.

lichray added inline comments.Dec 13 2017, 6:20 PM

Your test code prints 0x0 0x0 on FreeBSD though.
If reliable round-tripping is the goal, maybe we can enforce printing (nil)? fmtlib does this as well:

K-ballo abandoned this revision.Jan 14 2018, 1:45 PM