简体   繁体   English

Visual Studio检查迭代器而不是抛出异常

[英]Visual Studio checked iterator not throwing exceptions

I have this code: 我有这个代码:

#ifdef _DEBUG
#define _SECURE_SCL 1
#define _SECURE_SCL_THROWS 1
#else
#define _SECURE_SCL 0
#define _SECURE_SCL_THROWS 0
#endif

#include <iostream>
#include <vector>


using namespace std;




int main()
{
    vector <int> v1;
    v1.push_back(33);

    vector <int>::iterator it1 = v1.begin();

    try 
    {
        it1++;

        it1++;
    }
    catch (...) 
    {
        cout << "vector is empty!!" << endl;
    }

    return 0;
}

A pretty simple one: I try to go past-the-boundary and I want the exception to be caught. 一个非常简单的问题:我试图超越边界,我想要捕获异常。 However the program simply crashes with a "Debug Assertion Failed!" 但是程序只是因为“Debug Assertion Failed!”而崩溃了。 on the second increment, why is that? 在第二个增量,为什么?

The example was taken from http://msdn.microsoft.com/en-us/library/aa985965(v=vs.100).aspx and I just added the macros to set the checked iterators on. 该示例来自http://msdn.microsoft.com/en-us/library/aa985965(v=vs.100).aspx ,我刚刚添加了宏来设置已检查的迭代器。 I'm in debug mode, /EHsc is on and so is /MDd 我处于调试模式,/ EHsc打开,因此/ MDd

You left the most important part out of your question -- the #include lines. 你从问题中留下了最重要的部分 - #include行。

You need to put those macros ABOVE the #include lines. 您需要将这些宏放在#include行之上。 If you're using precompiled headers, you need to put them in the precompiled header. 如果您正在使用预编译头,则需要将它们放在预编译头中。

Because the code in your question is not complete, I can't tell if you've done this right. 因为你问题中的代码不完整,我不知道你是否做得对。 I suspect you haven't, so I'm offering this as an answer. 我怀疑你没有,所以我提供这个答案。

The new version of Visual C++'s library (bundled in Visual Studio 2010) doesn't support throwing exceptions from checked iterators. 新版本的Visual C ++库(捆绑在Visual Studio 2010中)不支持从已检查的迭代器中抛出异常。 See http://wishmesh.com/2010/04/it-seems-that-_secure_scl_throws-is-deprecated-in-visual-studio-c-2010/ http://wishmesh.com/2010/04/it-seems-that-_secure_scl_throws-is-deprecated-in-visual-studio-c-2010/

A checked iterator refers to an iterator that will throw an exception or call invalid_parameter if you attempt to move past the boundaries of the container. 已检查的迭代器是指如果您尝试移过容器边界,将抛出异常或调用invalid_parameter的迭代器。

Your example code isn't moving an iterator, so I don't think it makes sense that anything would be thrown. 你的示例代码没有移动迭代器,所以我认为不会抛出任何东西是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM