Skip to content

Commit c92c99e

Browse files
committed
Enabled forEach for const Mats
1 parent 2ac57a2 commit c92c99e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

modules/core/include/opencv2/core/mat.inl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ void Mat::forEach(const Functor& operation) {
12141214
template<typename _Tp, typename Functor> inline
12151215
void Mat::forEach(const Functor& operation) const {
12161216
// call as not const
1217-
(const_cast<Mat*>(this))->forEach<const _Tp>(operation);
1217+
(const_cast<Mat*>(this))->forEach<_Tp>(operation);
12181218
}
12191219

12201220
template<typename _Tp> inline

modules/core/test/test_mat.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,13 @@ struct InitializerFunctor5D{
671671
}
672672
};
673673

674+
template<typename Pixel>
675+
struct EmptyFunctor
676+
{
677+
void operator()(const Pixel &, const int *) const {}
678+
};
679+
680+
674681
void Core_ArrayOpTest::run( int /* start_from */)
675682
{
676683
int errcount = 0;
@@ -799,6 +806,17 @@ void Core_ArrayOpTest::run( int /* start_from */)
799806
}
800807
}
801808

809+
// test const cv::Mat::forEach
810+
{
811+
const Mat a(10, 10, CV_32SC3);
812+
Mat b(10, 10, CV_32SC3);
813+
const Mat & c = b;
814+
a.forEach<Point3i>(EmptyFunctor<Point3i>());
815+
b.forEach<Point3i>(EmptyFunctor<const Point3i>());
816+
c.forEach<Point3i>(EmptyFunctor<Point3i>());
817+
// tests compilation, no runtime check is needed
818+
}
819+
802820
RNG rng;
803821
const int MAX_DIM = 5, MAX_DIM_SZ = 10;
804822
// sparse matrix operations

0 commit comments

Comments
 (0)