Search code examples
c++opencvopencv3.3

Why does InputOutputArray take a const Mat& reference in the constructor?


The constructor for an InputOutputArray takes a const cv::Mat&in the constructor, meaning this compiles without any warnings or errors.

Why does the InputOutputArray take a const Mat reference. I understand why the InputArray takes a const reference, but an OutputArray really should not

void this_shouldnt_compile(const cv::Mat& mat) { 
    // const, so the header should not be modifiable
    // but the data is
    putText(mat, "some text", cvPoint(0,10), 
               FONT_HERSHEY_COMPLEX_SMALL, 0.8, 
               cvScalar({0,255,0}), 1, CV_AA);
}

Solution

  • So constructor for cv::_InputOutputArray::_InputOutputArray (const Mat & m) indeed exists

    By looking at https://github.com/opencv/opencv/issues/7298 it seems that

    1. "Access "locks" to underlying data are not available neither in compile time, nor during runtime. It is developer responsibility to control this"

    2. "... may be there is no real solution (even with c++17)"

    3. "Issues such as this are well known in C++, and are not unique to OpenCV."