Details
-
Improvement
-
Resolution: Fixed
-
Major - P3
-
None
-
Service Arch
-
Fully Compatible
-
Service Arch 2023-08-07
Description
Only having a conversion operator makes it awkward to work with the string data in the same scope where you declare the ItoA object and makes it tempting to do the wrong thing:
auto str = ItoA(someNum);
|
invariant(str.size() < 4); // doesn't compile |
invariant(str.operator StringData().size()); // compiles, but gross |
invariant(str.sd().size() < 4); // nice, but doesn't compile... yet :) |
|
|
// danger!
|
// We can't prevent this without also preventing the totally valid f(ItoA(someNum)),
|
// but we can make it less tempting.
|
auto str2 = StringData(ItoA(someNum));
|
As an alternative, we could make ItoA be a subclass of StringData so that it just directly presents the string-like API. It is a bit odd, since that would allow assigning through a mutable StringData& (but not through an ItoA& or an ItoA variable), but since that seems to be harmless, maybe its fine?