Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • Uff, the performance cost of this though. It would do a heap allocation for every call to a libfunction, even for multiple calls with the same argument literal. It would also require the external library to be stateless, lest it store an argument char* to a heap allocation that will be freed at the end of the call expression. Commented Feb 12, 2022 at 7:47
  • @Ramon Any idea, how to improve it? char array with fixed (maximal) width, then the heap allocations would be prevented. User-defined string literals do not seem to pass the parameters (char* and size_t) as compile-time constants. Is the solution at least safe from not generating a dangling pointer after the implicit conversion? Your second remark about storing the pointer depends on what the library does with the literals. Perhaps it is not an issue, perhaps it is. But then it would neither be allowed to pass local const* variables. Commented Feb 12, 2022 at 8:40
  • I really don't think you're gonna get any overhead on this as long as its evaluated at compile time. You might try to see if you can make it consteval instead of constexpr. That being said, iirc std::string has been made constexpr in C++20 so there really should be no problem just using a std::string. i.e. static std::string("string literal"); Commented Feb 13, 2022 at 1:21
  • @Taekahn: C++20 doesn’t allow allocations during constant evaluation to survive into runtime (and neither will C++23). Commented Feb 13, 2022 at 6:28