Reputation: 18483
I have this code (opencv 3.4.1):
void addText(const cv::Mat& mat)
{
cv::putText(mat, "test", cv::Point2f(100, 100), cv::FONT_HERSHEY_PLAIN, 2, cv::Scalar(0, 0, 255, 255));
}
I was expecting the compiler to refuse it, but to my surprise it compiles without any warning (I thought defining it as const would be enough to prevent any changes to the mat).
What would be the best practice to prevent any changes by mistake to a cv::Mat contents ?
Upvotes: 0
Views: 218
Reputation: 11805
See declaration of putText. And typedefs in mat.hpp
What's happening is that InputOutputArray is defined to be the same as OutputArray, and the latter has a non-explicit constructor from a const Mat reference.
Because of that, your Mat constness is effectively swallowed away. if you really care about const-correctness, don't use Mat's directly, wrap them and the OpenCV methods you call with an appropriate const-correct proxy/decorator.
Upvotes: 1