通过值返回boost ublas矩阵,仅在vs2012的发布配置中工作

returning a boost ublas matrix by value working only in release config for vs2012

本文关键字:布配置 配置 工作 vs2012 返回 boost ublas 仅在 矩阵      更新时间:2023-10-16

我正在使用visual studio 2012,我在我的代码中隔离了一个问题,但我无法解决它。当在发布模式下运行它时,它工作完美,但如果我在调试模式下运行它,我会得到一个错误。代码是:

#include "stdafx.h"
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
typedef boost::numeric::ublas::matrix<double> BMT; 
BMT fun()
  { 
  BMT mym;
  mym.resize(3,3);
  for(int i = 0; i<9;++i) mym(i/3,i%3)=i; 
  std::cout << mym << std::endl;
  return mym; 
  }
int main(int argc, char* argv[])
  {
  fun();
  //closing message
  std::cout<<std::endl<<"press enter to exit."<<std::endl;
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
  }

,调试时的错误如下:

1>------ Build started: Project: myproject, Configuration: Debug x64 ------
1>  myapp.cpp
1>C:Program Files (x86)Microsoft Visual Studio 11.0VCincludexmemory(348): error C4996: 'std::_Uninitialized_copy0': 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'
1>          C:Program Files (x86)Microsoft Visual Studio 11.0VCincludexmemory(333) : see declaration of 'std::_Uninitialized_copy0'
1>          C:thirdpartyvs2012x64boost_1_53_0boost/numeric/ublas/storage.hpp(94) : see reference to function template instantiation '_FwdIt std::uninitialized_copy<const double*,double*>(_InIt,_InIt,_FwdIt)' being compiled
1>          with
1>          [
1>              _FwdIt=double *,
1>              _InIt=const double *
1>          ]
1>          C:thirdpartyvs2012x64boost_1_53_0boost/numeric/ublas/storage.hpp(89) : while compiling class template member function 'boost::numeric::ublas::unbounded_array<T>::unbounded_array(const boost::numeric::ublas::unbounded_array<T> &)'
1>          with
1>          [
1>              T=double
1>          ]
1>          C:thirdpartyvs2012x64boost_1_53_0boost/numeric/ublas/matrix.hpp(160) : see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<T>::unbounded_array(const boost::numeric::ublas::unbounded_array<T> &)' being compiled
1>          with
1>          [
1>              T=double
1>          ]
1>          C:thirdpartyvs2012x64boost_1_53_0boost/numeric/ublas/matrix.hpp(100) : see reference to class template instantiation 'boost::numeric::ublas::unbounded_array<T>' being compiled
1>          with
1>          [
1>              T=double
1>          ]
1>          junkApp1.cpp(10) : see reference to class template instantiation 'boost::numeric::ublas::matrix<T>' being compiled
1>          with
1>          [
1>              T=double
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

你知道是什么问题吗?

我在使用ublas时遇到了类似的问题。我不确定原因可能是什么,但我想这与ublas的写时复制/按需复制优化有关。它是一个选项,只是使用定义-D_SCL_SECURE_NO_WARNINGS和完成它?我有一个全局设置,因为我认为90%的警告是OS特定的BS。

问题是您正在编译中将警告视为错误。

编辑:Microsoft已经决定c++(和C)的某些部分被弃用,并且编译器将这些部分的使用报告为错误,除非定义了_SCL_SECURE_NO_WARNINGS(分别为_CRT_SECURE_NO_WARNINGS)。