c++ - std::allocator_traits::construct with const 指针

标签 c++ c++11 c++14 language-lawyer allocator

下面的代码可以正常编译:

#include <iostream>
#include <memory>
int main()
{
const int * a = new int(5);

std::cout << *a << std::endl; // Prints 5

// *a = 5; // Compiler error.
using at = std::allocator_traits<typename std::allocator<int>>;
auto alloc = std::allocator<int>();
at::construct(alloc, a);

std::cout << *a << std::endl; // Prints 0
}

在 libstdc++ 的背后

::new((void*)a) int;

但是 aconst!

这是未定义的行为吗?或者 placement new 不算修改? 我修改了*a的值,是const。据我了解,这是不允许的:

Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior.

https://en.cppreference.com/w/cpp/language/const_cast

最佳答案

TL;DR:在 C++2a 之前都很好,此后 std::allocator_traits<std::allocator<int>>::construct()将对传递的指针要求更高。


嗯,std::allocator_traits::construct() uses static_cast<void*> since it was introduced ,除非分配器提供它。

同时 std::allocator::construct()在 C++17 中已弃用,并将在 C++2a 中删除,它始终使用 C 风格的转换。

因此,它在 C++2a 之前在语法上是有效的。


因为指向的对象本身不是 const ,只有通过具有该限定符才能访问的指针,丢弃 const并且修改是完全合法的。

作为 int 的伪驱动程序是微不足道的,在它上面构建一个新的之前不调用它甚至都没有关系。

关于c++ - std::allocator_traits::construct with const 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56942404/

相关文章:

c++ - 为什么 clang++ 编译的文件与 g++ 不同?

c++ - 在 OpenCV 中计算码本

java - Java 匿名回调类的 C++11 替代方案

c++ - 要导出的 Base unique_ptr vector

c++ - 在 C++ 中创建类型列表组合

c++ - 在实体组件系统中将实体与系统匹配的有效方法

c++ - 下面几行中 const 的用法是什么,如果我们不使用这些量怎么办?

c++ - 程序不会运行 while 语句消息

c++显式调用构造函数和临时对象

C++制作二维 bool 矩阵