NVIDIA / NVIDIA/cccl

Handle operators that accept lvalue references

Open
#908 1 comment 1 reaction 0 assignees View on GitHub
cub nvbug
Dominant language
C++
Stars
2.5k
Forks
487
Avg merge
2d 7h
Merged PRs (30d)
296

Description

Since 2.0 we compute accumulator type for algorithms like reduce and scan as `invoke_result_t`. This leads to compilation issues when operator accepts lvalue references:
```cpp
#include

struct op_t {
public:
__host__ __device__ int operator()(int &v1, int &v2) {
return v1 * v2;
}
};

int main() {
static_assert(std::is_same_v, int>);
}
```

Having lvalue references shouldn't be allowed, since we assume that arguments are not modified when invoking the operator. Algorithms like `std::accumulate` also require:
> `op` must not modify any elements of the range involved. The signature of the function should be equivalent to the following:
> ```cpp
> Ret fun(const Type1 &a, const Type2 &b);
> ```

In our documentation we explicitly require operator to have `T operator()(const T &a, const T &b)` method. We should adjust accumulator type computation to pass const references into `invoke_result_t`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.