[SERVER-44049] Allow public constexpr StringData constructors Created: 16/Oct/19  Updated: 29/Oct/23  Resolved: 08/May/20

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

Type: New Feature Priority: Major - P3
Reporter: Benjamin Caimano (Inactive) Assignee: Billy Donahue
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-46503 inline namespace mongo::literals for ... Closed
Backwards Compatibility: Fully Compatible
Sprint: Service arch 2020-05-18
Participants:

 Description   

StringData currently uses a private tag pattern for its constexpr ctor. Since modern compilers can throw from constexpr ctors, we should be able to make the public constructors safely constexpr now.



 Comments   
Comment by Githook User [ 08/May/20 ]

Author:

{'name': 'Billy Donahue', 'email': 'billy.donahue@mongodb.com', 'username': 'BillyDonahue'}

Message: SERVER-44049 constexpr StringData(ptr,len) constructor
Branch: master
https://github.com/mongodb/mongo/commit/38ccc532cd15d1cc5f6cc6c4ddc0be2269c4454a

Comment by Billy Donahue [ 05/May/20 ]

I have run into a need for this in the logv2 attribute static checking as well. SERVER-47933

Comment by Billy Donahue [ 30/Apr/20 ]

PoC w/Tests: http://mongodbcr.appspot.com/590660001

Comment by Billy Donahue [ 30/Apr/20 ]

This feature would have helped me implement "SERVER-46503 inline namespace mongo::literals" more easily.

when the StringData literal operator""_sd moved to namespace mongo::literals, it was awkward to make it a friend of mongo::StringData. It would have required friendship across namespaces and a forward declaration.

I chose to work around the lack of constexpr StringData ctors
by introducing an embarrassing public helper function for the operator"" to use.

+    /** Helper for the `literals::operator""_sd` function below. Conceptually non-public. */
+    static constexpr StringData _literalHelper(const char* c, std::size_t len) {
+        return StringData(c, len, TrustedInitTag{});
+    }

This is not a good solution. The right way to do it is to have a public constexpr constructor.

Comment by Mira Carey [ 29/Oct/19 ]

We don't immediately need this for anything. Feel free to reopen if something comes up in the future

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