phobos
phobos copied to clipboard
Improve std.container.util.make with forwarding and clearer compile-time errors
The template make in std.container.util could be improved in a few ways.
- Preserve move semantics using
std.algorithm.mutation.forwardwhen passing arguments to constructors. Currently arguments are passed directly, which may cause unnecessary copies.
Current code: return T(arguments);
Suggested improvement: import std.algorithm.mutation : forward; return T(forward!arguments);
- Improve compile-time diagnostics by adding a
static assertwhen the constructor cannot be called with the provided arguments.
Example: static assert(__traits(compiles, T(arguments)), "Cannot construct "~T.stringof~" with given arguments");
-
Improve the documentation of
maketo clarify that it abstracts the difference between constructing structs (T(args)) and classes (new T(args)). -
Add additional edge-case unit tests (e.g., empty container construction).
If this approach is acceptable, I would be happy to submit a PR implementing these improvements.