c++ - Visual Studio 2015 错误 C4996 'std::_Copy_impl' : Function call with parameters that may be unsafe

标签 c++ opencv c++11 visual-studio-2015

我写了这个模板类:

.hpp:

#ifndef BIVARIATEGAUSSIANPDF_LIB_H
#define BIVARIATEGAUSSIANPDF_LIB_H

#if defined BIVARIATEGAUSSIANPDF_LIB_EXPORTS
#define BIVARIATEGAUSSIANPDFDLL_API __declspec(dllexport)
#else
#define BIVARIATEGAUSSIANPDFDLL_API __declspec(dllimport)
#endif

#include <vector>
#include <opencv2\core\core.hpp>

namespace cv
{
    class Mat;
}

#define _USE_MATH_DEFINES
#include <math.h>

template<class T, class N>
class BivariateGaussianPDF
{
public:
    BIVARIATEGAUSSIANPDFDLL_API BivariateGaussianPDF(const std::vector<T> & i_v1,
                                                     const std::vector<N> & i_v2);

    BIVARIATEGAUSSIANPDFDLL_API ~BivariateGaussianPDF();

    BIVARIATEGAUSSIANPDFDLL_API void compute();

    BIVARIATEGAUSSIANPDFDLL_API inline std::vector<double> getPDF() const { return m_pdf; }

private:
    cv::Mat m_matrix;
    std::vector<double> m_pdf;
};

template class BivariateGaussianPDF<int, int>;
template class BivariateGaussianPDF<int, float>;
template class BivariateGaussianPDF<int, double>;
template class BivariateGaussianPDF<float, int>;
template class BivariateGaussianPDF<float, float>;
template class BivariateGaussianPDF<float, double>;
template class BivariateGaussianPDF<double, int>;
template class BivariateGaussianPDF<double, float>;
template class BivariateGaussianPDF<double, double>;

#endif

和.cpp:

#include "stdafx.h"
#include "bivariateGaussianPDF_lib.h"
#include <iterator>

template<class T, class N>
BivariateGaussianPDF<T, N>::BivariateGaussianPDF(const std::vector<T>& i_v1,
                                                 const std::vector<N>& i_v2)
    :m_pdf(i_v1.size())
{
    std::vector<double> temp_v1(std::begin(i_v1), std::end(i_v1));
    std::vector<double> temp_v2(std::begin(i_v2), std::end(i_v2));
    m_matrix.create(2, i_v1.size(), CV_64F);
    double * r0_ptr = m_matrix.ptr<double>(0);
    double * r1_ptr = m_matrix.ptr<double>(1);
    std::copy(temp_v1.begin(), temp_v1.end(), r0_ptr);
    std::copy(temp_v2.begin(), temp_v2.end(), r1_ptr);
}

template<class T, class N>
BivariateGaussianPDF<T, N>::~BivariateGaussianPDF()
{
    m_matrix.release();
}

template<class T, class N>
void BivariateGaussianPDF<T, N>::compute()
{
    cv::Mat row_mean, cov, inverseCov, temp_pdf;

    // compute covariance matrix
    cv::calcCovarMatrix(m_matrix, cov, row_mean, CV_COVAR_ROWS | CV_COVAR_NORMAL);
    // scale covariance matrix
    cov = cov / (m_matrix.cols - 1);
    // inverse of covariance matrix
    inverseCov = cov.inv();

    // determinant of covariance matrix
    const double det = cv::determinant(cov);

    // X = [x1; x2 ... ; xN] --> [x1 - mu1; x2 - mu2; ... ; xN - muN ]
    cv::subtract(m_matrix.row(0), row_mean.row(0), m_matrix.row(0));
    cv::subtract(m_matrix.row(1), row_mean.row(1), m_matrix.row(1));

    // (inv*X).*X
    cv::reduce((inverseCov*m_matrix).mul(m_matrix), temp_pdf, 0, CV_REDUCE_SUM);
    temp_pdf = -0.5*temp_pdf;
    cv::exp(temp_pdf, temp_pdf);

    // bivariate gaussian pdf
    temp_pdf = (0.5 / (M_PI*std::sqrt(det)))*temp_pdf;

    const double * d_ptr = temp_pdf.ptr<double>(0);
    std::copy(d_ptr, d_ptr + temp_pdf.cols, m_pdf.begin());
}

当我编译时,我收到这条消息:

Error C4996 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' bivariateGaussianPDF_lib c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 2229

阅读 this answer我明白了,因为我得到了错误(我没有根据 msdn 函数签名调用 std::copy)但我不知道如何修复它。我认为错误行是:

std::copy(temp_v1.begin(), temp_v1.end(), r0_ptr);
std::copy(temp_v2.begin(), temp_v2.end(), r1_ptr);

感谢任何帮助。

最佳答案

我按照说明解决了问题 in this article @DanMašek 在评论中建议。

我添加了标题 <iterator>然后我使用了类 stdext::checked_array_iterator .结果是这样的:

    double * r0_ptr = m_matrix.ptr<double>(0);
    std::copy(temp_v1.begin(), temp_v1.end(), stdext::checked_array_iterator<double *>(r0_ptr, m_matrix.cols));

关于c++ - Visual Studio 2015 错误 C4996 'std::_Copy_impl' : Function call with parameters that may be unsafe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36409156/

相关文章:

c++ - move 赋值运算符和 `if (this != &rhs)`

c++ - visual c++ 未声明的标识符

c++ - 什么样的数据或函数放在C++类的保护区比较好?

c++ - AOT编译语言是如何实现匿名函数的?

c++ - 连接两个大 QVector 并对它们进行排序的最快方法 (C++/Qt)

c++ - OpenCV:从 std::in 读取图像?

c++ - 为 apache 编写基于 C++11 线程的模块

python - knnMatch 不适用于 K != 1

python - 使用多处理时 cv2.Boost 的 Pickle 异常

c++ - 在 C++11 中从 std::deque move 一个元素