Cannot call find with haystack elements having a explicit copy constructors

Per Nordlöw per.nordlow at gmail.com
Thu Jul 15 11:08:25 UTC 2021


The adding of copy construtors to `Service` defined as

```d
@safe struct Service {
     this(ref return scope typeof(this) rhs) {}
     this(const ref return scope typeof(this) rhs) const {}
}

@safe struct Session {
     void openAndGetService(in string key) scope {
         import std.algorithm.searching : find;
         auto hit = _pairs.find!((x) => x.key == key)();
     }
     private Pair[] _pairs;
     private struct Pair {
         string key;
         Service service;
     }
}
```

makes a call to find error as

```
copy_ctor_find_fail.d(9): Error: template 
`std.algorithm.searching.find` cannot deduce function from 
argument types `!((x) => x.key == key)(Pair[])`, candidates are:
auto hit = _pairs.find!((x) => x.key == key)();
^
std/algorithm/searching.d(1559):        `find(alias pred = "a == 
b", InputRange, Element)(InputRange haystack, scope Element 
needle)`
InputRange find(alias pred = "a == b", InputRange, 
Element)(InputRange haystack, scope Element needle)
^
std/algorithm/searching.d(1827):        `find(alias pred, 
InputRange)(InputRange haystack)`
with `pred = __lambda2,
InputRange = Pair[]`
must satisfy the following constraint:
`       isInputRange!InputRange`
InputRange find(alias pred, InputRange)(InputRange haystack)
^
std/algorithm/searching.d(1881):        `find(alias pred = "a == 
b", R1, R2)(R1 haystack, scope R2 needle)`
R1 find(alias pred = "a == b", R1, R2)(R1 haystack, scope R2 
needle)
^
std/algorithm/searching.d(2340):        `find(alias pred = "a == 
b", Range, Ranges...)(Range haystack, Ranges needles)`
with `pred = __lambda2,
Range = Pair[],
Ranges = ()`
must satisfy the following constraint:
`       Ranges.length > 1`
Tuple!(Range, size_t) find(alias pred = "a == b", Range, 
Ranges...)
^
std/algorithm/searching.d(2455):        `find(RandomAccessRange, 
alias pred, InputRange)(RandomAccessRange haystack, scope 
BoyerMooreFinder!(pred, InputRange) needle)`
RandomAccessRange find(RandomAccessRange, alias pred, InputRange)(
^```
Using

```d
auto hit = _pairs.find!((scope const ref x) => x.key == key)();
```

fails in the same way. What's wrong?



More information about the Digitalmars-d-learn mailing list