用VS2013运行boost::make_function_output_iterator的官方例子: 
https://www.boost.org/doc/libs/1_68_0/libs/iterator/doc/function_output_iterator.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
struct string_appender
{
    string_appender(std::string& s)
        : m_str(&s)
    {}
 
    void operator()(const std::string& x) const
    {
        *m_str += x;
    }
 
    std::string* m_str;
};
 
int main(intchar*[])
{
  std::vector<std::string> x;
  x.push_back("hello");
  x.push_back(" ");
  x.push_back("world");
  x.push_back("!");
 
  std::string s = "";
  std::copy(x.begin(), x.end(),
            boost::make_function_output_iterator(string_appender(s)));
 
  std::cout << s << std::endl;
 
  return 0;
}

结果报如下的编译错误:

 
1
Error    1    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'    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility    2132    1   

 

[解决方法]

加入预处理器(项目属性----C/C++----预处理----预处理器定义):
_SCL_SECURE_NO_WARNINGS

[分析]

这个编译错误是由于vc++的checked iterators规则(https://msdn.microsoft.com/en-us/library/aa985965.aspx), 加入_SCL_SECURE_NO_WARNINGS预处理器后, 编译会通过,然后将风险交给运行时,由于boost库比较完善,所以这种风险应该不存在.

把链接中的Checked iterators内容拷贝如下:

Checked Iterators

Visual Studio 2015
 
Other Versions
 

Checked iterators ensure that the bounds of your container are not overwritten. Checked iterators apply to both release builds and debug builds. For more information about how to use debug iterators when you compile in debug mode, see Debug Iterator Support.

Remarks

 
 

For information about how to disable warnings that are generated by checked iterators, see _SCL_SECURE_NO_WARNINGS.

You can use the _ITERATOR_DEBUG_LEVEL preprocessor macro to enable or disable the checked iterators feature. If _ITERATOR_DEBUG_LEVEL is defined as 1 or 2, unsafe use of iterators causes a runtime error and the program is terminated. If defined as 0, checked iterators are disabled. By default, the value for _ITERATOR_DEBUG_LEVEL is 0 for release builds and 2 for debug builds.

 Important

Older documentation and source code may refer to the _SECURE_SCL macro. Use _ITERATOR_DEBUG_LEVEL to control _SECURE_SCL. For more information, see _ITERATOR_DEBUG_LEVEL.

When _ITERATOR_DEBUG_LEVEL is defined as 1 or 2, these iterator checks are performed:

  • All standard iterators (for example, vector::iterator) are checked.

  • If an output iterator is a checked iterator, calls to standard library functions such as std::copy get checked behavior.

  • If an output iterator is an unchecked iterator, calls to standard library functions cause compiler warnings.

  • The following functions generate a runtime error if there is an access that is outside the bounds of the container:

When _ITERATOR_DEBUG_LEVEL is defined as 0:

  • All standard iterators are unchecked (iterators can move beyond the container boundaries, which leads to undefined behavior).

  • If an output iterator is a checked iterator you will get checked behavior on calls to the standard function (for example, std::copy).

  • If an output iterator is an unchecked iterator you will get unchecked behavior on calls to the standard function (for example, std::copy).

A checked iterator refers to an iterator that will call invalid_parameter_handler if you attempt to move past the boundaries of the container. For more information about invalid_parameter_handler, see Parameter Validation.

checked_array_iterator Class and unchecked_array_iterator Class are the iterator adaptors that support checked iterators.

Example

 
 

When you compile by using _ITERATOR_DEBUG_LEVEL set to 1 or 2, a runtime error will occur if you attempt to access an element that is outside the bounds of the container by using the indexing operator of certain classes.

 
// checked_iterators_1.cpp
// cl.exe /Zi /MDd /EHsc /W4 #define _ITERATOR_DEBUG_LEVEL 1 #include <vector>
#include <iostream> using namespace std; int main()
{
vector<int> v;
v.push_back(67); int i = v[0];
cout << i << endl; i = v[1]; //triggers invalid parameter handler
}

This program prints "67" then pops up an assertion failure dialog box with additional information about the failure.

Example

 
 

Similarly, when you compile by using _ITERATOR_DEBUG_LEVEL set to 1 or 2, a runtime error will occur if you attempt to access an element by usingfront or back in container classes when the container is empty.

 
// checked_iterators_2.cpp
// cl.exe /Zi /MDd /EHsc /W4
#define _ITERATOR_DEBUG_LEVEL 1 #include <vector>
#include <iostream> using namespace std; int main()
{
vector<int> v; int& i = v.front(); // triggers invalid parameter handler
}

This program pops up an assertion failure dialog box with additional information about the failure.

Example

 
 

The following code demonstrates various iterator use-case scenarios with comments about each. By default, _ITERATOR_DEBUG_LEVEL is set to 2 in Debug builds, and to 0 in Retail builds.

 
// checked_iterators_3.cpp
// cl.exe /MTd /EHsc /W4 #include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <vector> using namespace std; template <typename C>
void print(const string& s, const C& c)
{
cout << s; for (const auto& e : c) {
cout << e << " ";
} cout << endl;
} int main()
{
vector<int> v(16);
iota(v.begin(), v.end(), 0);
print("v: ", v); // OK: vector::iterator is checked in debug mode
// (i.e. an overrun causes a debug assertion)
vector<int> v2(16);
transform(v.begin(), v.end(), v2.begin(), [](int n) { return n * 2; });
print("v2: ", v2); // OK: back_insert_iterator is marked as checked in debug mode
// (i.e. an overrun is impossible)
vector<int> v3;
transform(v.begin(), v.end(), back_inserter(v3), [](int n) { return n * 3; });
print("v3: ", v3); // OK: array::iterator is checked in debug mode
// (i.e. an overrun causes a debug assertion)
array<int, 16> a4;
transform(v.begin(), v.end(), a4.begin(), [](int n) { return n * 4; });
print("a4: ", a4); // OK: Raw arrays are checked in debug mode
// (an overrun causes a debug assertion)
// NOTE: This applies only when raw arrays are given to C++ Standard Library algorithms!
int a5[16];
transform(v.begin(), v.end(), a5, [](int n) { return n * 5; });
print("a5: ", a5); // WARNING C4996: Pointers cannot be checked in debug mode
// (an overrun causes undefined behavior)
int a6[16];
int * p6 = a6;
transform(v.begin(), v.end(), p6, [](int n) { return n * 6; });
print("a6: ", a6); // OK: stdext::checked_array_iterator is checked in debug mode
// (an overrun causes a debug assertion)
int a7[16];
int * p7 = a7;
transform(v.begin(), v.end(), stdext::make_checked_array_iterator(p7, 16), [](int n) { return n * 7; });
print("a7: ", a7); // WARNING SILENCED: stdext::unchecked_array_iterator is marked as checked in debug mode
// (it performs no checking, so an overrun causes undefined behavior)
int a8[16];
int * p8 = a8;
transform(v.begin(), v.end(), stdext::make_unchecked_array_iterator(p8), [](int n) { return n * 8; });
print("a8: ", a8);
}

When you compile this code by using cl.exe /EHsc /W4 /MTd checked_iterators_3.cpp the compiler emits a warning, but compiles without error into an executable:

 
algorithm(1026) : warning C4996: 'std::_Transform1': 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'

When run at the command line, the executable generates this output:

 
v: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
v2: 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
v3: 0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45
a4: 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
a5: 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
a6: 0 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90
a7: 0 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105
a8: 0 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120

boost::make_function_output_iterator报错: C4996的更多相关文章

  1. boost pool_allocator 报错 'rebind'

    #include "stdafx.h" #include <vector> #include <boost/pool/pool.hpp> int _tmai ...

  2. 在Visual Studio 2019中使用scanf报错C4996解决办法

    错误警告信息 错误C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To ...

  3. C++解决error C4996报错

    今天用c++写了个数独程序,在编译过程中报了一个错误: 1>------ 已启动生成: 项目: sudoku, 配置: Debug Win32 ------1> main.cpp1> ...

  4. GCC 高版本7.4 编译链接 boost 报错 boost::thread::XXX’未定义的引用 解决方法

    背景:开发中的项目之前一直用GCC4.8,boost库1.48版本的开发环境.现在因业务需求,需要更换GCC7.4,boost库1.70. 问题:可以正常编译BOOST的链接库文件,但是链接时候报错. ...

  5. 16061701(地图灯光编译Beast报错)

    [目标] 地图灯光编译报错 [思路] 1 我自己测c2_cwd_rt 附件为当时log 2 ExampleGame\BeastCache\PersistentCache 3 重新删除掉BeastCac ...

  6. caffe ubuntu16安装报错和程序总结

    我最近安装安装了老版本的caffe,安装过程真是两个字"想死",所以我的错误一般都是比较经典的. 我是使用cuda的版本,所以可能会出现undefined refference t ...

  7. caffe编译报错解决

    添加ssd中的一些层之后,编译报错: ../lib/libcaffe.so.1.0.0-rc5:对‘boost::match_results<__gnu_cxx::__normal_iterat ...

  8. 【PaddlePaddle系列】报错解决方法合集 (不定时更新)

    1.PaddlePaddle使用CPU时正常运行,但是使用GPU时却报出一堆错误信息,节选如下: paddle.fluid.core.EnforceNotMet: enforce allocating ...

  9. 。。。。。。不带http https : 不报错 spring boot elasticsearch rest

    ......不带http https  : 不报错 先telnet http://onf:8080/getES653/道路桥梁正在“理疗”%20这14条道路纳入市政中修 @GetMapping(&qu ...

随机推荐

  1. Vue2.0学习——axios用法详解

    功能特性 在浏览器中发送 XMLHttpRequests 请求 在 node.js 中发送 http请求 支持 Promise API 拦截请求和响应 转换请求和响应数据 自动转换 JSON 数据 客 ...

  2. AtCoder Regular Contest 099 (ARC099) E - Independence 二分图

    原文链接https://www.cnblogs.com/zhouzhendong/p/9224878.html 题目传送门 - ARC099 E - Independence 题意 给定一个有 $n$ ...

  3. excel表格导入数据库数据存在则更新不存在添加

    public void excelToDB() throws ParseException { String datapath = this.getParameter("datapath&q ...

  4. Raspberry Camera详解+picamera库+Opencv控制

    使用树莓派的摄像头,将树莓派自身提供的picamera的API数据转换为Python Oencv可用图像数据: # import the necessary packages from picamer ...

  5. Sort功能极强!

    Sort功能极强! 可以排string:  sort(a.begin(),a.end()); 普通数组 结合结构体 逆序 而且贼快

  6. 001.Amoeba读写分离部署

    一 Amoeba简介 Amoeba(变形虫)项目,该开源框架于2008年 开始发布一款 Amoeba forMysql软件.这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQ ...

  7. Rendering React components to the document body

    React一个比较好用的功能是其简单的API,一个组件可以简单到一个return了组件结构的render函数.除了一个简单的函数之外,我们还有了一段有用且可复用的代码片段. 问题 不过有时候可能会受到 ...

  8. linux 学习笔记四

    文件权限管理篇章 chown  -R mysql.  /usr/local 把 /usr/local/以及以下的所有文件和子目录属主改为  mysql ls -al * 用这条命令查询 chgrp c ...

  9. VsVim的快捷键

    VsVim - Shortcut Key (快捷键) Enable / Disable 还可以通过 Ctrl+Shift+F12 在 Visual Studio 中实现 Enable / Disabl ...

  10. Spring配置JDBCTemplate

    案例:单测查询全部学生 项目结构: 1.导入部署jar包:spring-jdbc <!--spring-jdbc--> <dependency> <groupId> ...