gpt4 book ai didi

c++11 - 在 VS 2015 中为 const vector 报告了编译器错误,但在 VS 2013 中没有

转载 作者:行者123 更新时间:2023-12-03 20:58:55 26 4
gpt4 key购买 nike

以下代码使用 Visual Studio 2013 编译正常。

#include <vector>
#include <string>

int main()
{
const std::string constString("fred");
const std::vector<const std::string> myVector{ constString };
}

如果我尝试使用 Visual Studio 2015 编译它,则会报告以下错误:
1>xmemory0(587): error C2338: The C++ Standard forbids containers of const elements because allocator<const T> is ill-formed.
我看过各种帖子,尤其是这个 Does C++11 allow vector<const T>? , 关于 vector<const T>以及为什么不允许这样做,但我真的不明白。然而,在上面的例子中,向量本身是常量。

有人可以解释一下吗? VS 2013 编译成功是错误的吗?

最佳答案

Standard   Requirement for T
======== =================
C++03 any type
C++11 any non-const, non-reference object type
C++14 any non-const object type
C++17 any cv-unqualified object type

标准中的任何地方都没有异常(exception)
当容器本身是 const
所以是的,VS 2013 编译成功是错误的。

关于c++11 - 在 VS 2015 中为 const vector<const T> 报告了编译器错误,但在 VS 2013 中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32864987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com