Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Nothrow move constructors for Point, Size, Point3, Rect

The static asserts currently fail with opencv-3.0.0 with gcc (GCC) 5.1.1 20150618 (Red Hat 5.1.1-4)

#include <type_traits>                  // for is_nothrow_move_constructible
#include <vector>                       // for vector
#include "opencv2/core/types.hpp"       // for Point2d, Rect2d, etc.

struct x_polyline {
    cv::Point2d start;
    std::vector<double> polyline;

    x_polyline(const x_polyline&) = default;
    x_polyline(x_polyline&& rhs) = default;

int main()
    static_assert(std::is_nothrow_move_constructible<cv::Point2d>::value, "cv::Point2d move constructor could throw");
    static_assert(std::is_nothrow_move_constructible<cv::Point3d>::value, "cv::Point3d move constructor could throw");
    static_assert(std::is_nothrow_move_constructible<cv::Size2d>::value, "cv::Size2d move constructor could throw");
    static_assert(std::is_nothrow_move_constructible<cv::Rect2d>::value, "cv::Rect2d move constructor could throw");

    static_assert(std::is_nothrow_move_constructible<x_polyline>::value, "x_polyline move constructor could throw");

    return 0;

The current hand-written copy constructors for these simple classes block the compiler from generating its own move constructors. As the example above shows, this applies to any class that includes them. Such classes will be hard to use in a std::vector, which decides whether to move or copy its contents on reallocation by testing this type trait (using std::move_if_noexcept).

If the copy constructors for cv::Point_, cv::Size_, cv::Point3_ and cv::Rect_ are commented out, or set to "= default", then the asserts will not fail. Would it be possible to do this for a future release of openCV?