Skip to content

Commit 8760d0c

Browse files
authored
Merge pull request NVIDIA#1423 from jrhemstad/fix-transform-iterator-noncopyable
Fix transform_iterator with non-copyable types
2 parents 13e608f + 8355fa6 commit 8760d0c

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

testing/transform_iterator.cu

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <thrust/sequence.h>
88
#include <thrust/iterator/counting_iterator.h>
99

10+
#include <memory>
11+
1012
template <class Vector>
1113
void TestTransformIterator(void)
1214
{
@@ -84,3 +86,28 @@ struct TestTransformIteratorReduce
8486
};
8587
VariableUnitTest<TestTransformIteratorReduce, IntegralTypes> TestTransformIteratorReduceInstance;
8688

89+
90+
struct ExtractValue{
91+
int operator()(std::unique_ptr<int> const& n){
92+
return *n;
93+
}
94+
};
95+
96+
void TestTransformIteratorNonCopyable(){
97+
98+
thrust::host_vector<std::unique_ptr<int>> hv(4);
99+
hv[0].reset(new int{1});
100+
hv[1].reset(new int{2});
101+
hv[2].reset(new int{3});
102+
hv[3].reset(new int{4});
103+
104+
auto transformed = thrust::make_transform_iterator(hv.begin(), ExtractValue{});
105+
ASSERT_EQUAL(transformed[0], 1);
106+
ASSERT_EQUAL(transformed[1], 2);
107+
ASSERT_EQUAL(transformed[2], 3);
108+
ASSERT_EQUAL(transformed[3], 4);
109+
110+
}
111+
112+
DECLARE_UNITTEST(TestTransformIteratorNonCopyable);
113+

thrust/iterator/transform_iterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ template <class AdaptableUnaryFunction, class Iterator, class Reference = use_de
312312
// Create a temporary to allow iterators with wrapped references to
313313
// convert to their value type before calling m_f. Note that this
314314
// disallows non-constant operations through m_f.
315-
typename thrust::iterator_value<Iterator>::type x = *this->base();
315+
typename thrust::iterator_value<Iterator>::type const& x = *this->base();
316316
return m_f(x);
317317
}
318318

0 commit comments

Comments
 (0)