Using non-copyable types
H. S. Teoh
hsteoh at quickfur.ath.cx
Thu May 23 18:18:02 UTC 2019
On Thu, May 23, 2019 at 06:05:41PM +0000, Yuxuan Shui via Digitalmars-d wrote:
> When I try to use non-copyable types, I just get all sorts of
> resistance from the standard library.
>
> For example, most of the std.range functions assume the range is
> copyable. std.typecons.Tuple assume all of its types are copyable.
>
> I tried to use the new copy constructor feature to make a wrapper that
> moves by default. But apparently copy constructors are ignored when
> postblit is disabled.
>
> How can we make using non-copyable types easier?
Wrap them in a reference. I.e., instead of passing the range itself,
pass a pointer to the range. Or use a suitable wrapper struct with
reference semantics. Thanks to D unifying member invocation via object
and via pointer to object, this should be mostly transparent to Phobos
algorithms. This gets around the complications with postblits / copy
ctors and Phobos assumptions about copyability.
NonCopyableRange r = ...;
auto rp = &r;
result = rp.map!(...).filter!(...).joiner; // etc.
T
--
When solving a problem, take care that you do not become part of the problem.
More information about the Digitalmars-d
mailing list