[SERVER-41552] LogStreamBuilder unconstrained operator<< doesn't cover expected set of types. Created: 05/Jun/19  Updated: 27/Jun/19  Resolved: 27/Jun/19

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: Backlog
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Billy Donahue Assignee: Mira Carey
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Sprint: Service Arch 2019-06-17, Service Arch 2019-07-01
Participants:

 Description   

https://github.com/mongodb/mongo/blob/22a595fbef4d023002b3c74d529368fcc4dbb196/src/mongo/logger/logstream_builder.h#L203

Among the many overloads of LogStreamBuilder::operator<<,
we have this catch-all.

template <typename T>
    LogstreamBuilder& operator<<(const T& x) {
        stream() << x.toString();
        return *this;
    }

The problem is that it's not constrained to T for which x.toString() exists.
So it matches types it shouldn't, and they then fail to compile. It can also hide other operators that should be good matches due to implicit conversions or base classes.

We could probably do much better here.

Maybe if x.toString() is valid, we allow this into the overload set.

Otherwise, if std::ostream<<x is valid, we do stream()<<x, which a lot of the other overloads do.

Otherwise, LogStreamBuilder butts out and doesn't provide an overload for the operator. There's still a chance for x to define its own operator<< for LogStreamBuilder to be found by ADL, but that's not LogStreamBuilder's concern.

This strategy would reduce user surprise, and reduce the number of overloads in LogStreamBuilder from its current (insufficient) 24 down to something like 3.



 Comments   
Comment by Mira Carey [ 27/Jun/19 ]

Closing this as wontfix, as the cr was more contentious than expected and the value is limited (given our move to a new logging format in future)

Generated at Thu Feb 08 04:58:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.