简体   繁体   English

为什么checked_array_iterator在没有包含的情况下工作 <iterator> 在VS2013但在VS2017中失败了?

[英]Why does checked_array_iterator work without including <iterator> in VS2013 but fails in VS2017?

Given the following code: 给出以下代码:

#include <memory>
#include <locale>

int main() {
    auto source = std::make_unique<int[]>(16);
    auto dest = std::make_unique<int[]>(16);
    auto dp = stdext::checked_array_iterator<int*>(dest.get(), 16);
    std::copy_n(source.get(), 16, dp);
    return 0;
}

It compiles cleanly on Visual Studio 2013 by running cl.exe /EHsc <file>.cpp . 它通过运行cl.exe /EHsc <file>.cpp在Visual Studio 2013上完全编译。 However, on Visual Studio 2017 the following errors (among others) are thrown by cl.exe : 但是,在Visual Studio 2017上, cl.exe会抛出以下错误(以及其他错误):

vc12.cpp(7): error C2653: 'stdext': is not a class or namespace name
vc12.cpp(7): error C2065: 'checked_array_iterator': undeclared identifier

Why does this code no longer compile? 为什么这段代码不再编译?

The example is missing an #include <iterator> . 该示例缺少#include <iterator> Technically, it was also missing for Visual Studio 2013 (see Why include what you use ), but due to the chain of includes it worked there. 从技术上讲,Visual Studio 2013也缺少它(请参阅为什么包含您使用的内容 ),但由于其中包含了一系列包含功能。 Between Visual Studio 2013 and Visual Studio 2017 the includes of the std -headers got revamped. 在Visual Studio 2013和Visual Studio 2017之间, std -headers的包含得到了改进。

The example shows #include <locale> . 该示例显示了#include <locale> In the old version, #include <iterator> was part of the include-chain of locale , which is no longer the case in Visual Studio 2017. 在旧版本中, #include <iterator>locale包含链的一部分,在Visual Studio 2017中不再是这种情况。

However, the documentation of VS2017 is hard to find at the moment since it is so new. 然而,目前很难找到VS2017的文档,因为它是如此新颖。 The important document can be found at doc.microsoft.com , it lists the required header <iterator> . 重要文档可以在doc.microsoft.com找到,它列出了必需的头文件<iterator>

This is included in according to the documentation: https://docs.microsoft.com/en-us/cpp/standard-library/checked-array-iterator-class However you'll need to #include <iterator> . 这包含在根据文档: https//docs.microsoft.com/en-us/cpp/standard-library/checked-array-iterator-class但是你需要#include <iterator>

Though rather than pursuing correcting this, I'd entourage writing standard conformant code. 虽然不是在追求纠正这一点,但我还是会随意编写符合标准的符号代码。 The best way to do that would be to just use a statically allocated array. 最好的方法是使用静态分配的数组。 This will allow you to use C++'s begin , end , and size functions to work with it: https://stackoverflow.com/a/33442842/2642059 这将允许您使用C ++的beginendsize函数来使用它: https//stackoverflow.com/a/33442842/2642059

There are some cases where that wouldn't be a good suggestion, if a dynamic array is a must have, consider using a vector . 在某些情况下,这不是一个好建议,如果动态数组是必须的,请考虑使用vector If you can't abide a container then using a unique_ptr is a good way to do this, but rather than depending upon checked_array_iterator prefer maintaining your own size: 如果你不能遵守容器,那么使用unique_ptr是一个很好的方法,但不是依赖checked_array_iterator更喜欢保持自己的大小:

const size_t sourceSize = 16;
auto source = std::make_unique<int[]>(sourceSize);
auto dest = std::make_unique<int[]>(sourceSize);
std::copy_n(source.get(), sourceSize, dest.get())

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

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