【发布时间】:2017-07-06 23:22:59
【问题描述】:
在 g++ 中,const std::vector 的元素访问运算符定义如下:(/usr/include/c++/7.1.1/bits/stl_vector.h)
/**
* @brief Subscript access to the data contained in the %vector.
* @param __n The index of the element for which data should be
* accessed.
* @return Read-only (constant) reference to data.
*
* This operator allows for easy, array-style, data access.
* Note that data access with this operator is unchecked and
* out_of_range lookups are not defined. (For checked lookups
* see at().)
*/
const_reference
operator[](size_type __n) const _GLIBCXX_NOEXCEPT
{
__glibcxx_requires_subscript(__n);
return *(this->_M_impl._M_start + __n);
}
但是下面的代码使用g++ -c const-vector.cpp -Wall -pedantic编译没有问题
#include <vector>
void sum(const std::vector<int>& data){
int *ptr;
ptr = (int*) &data[0];
if (data.size()>2)
for (unsigned int i=1;i<data.size();i++)
ptr[0]+=ptr[i];
return;}
所以我正在更改由 const 引用传递的 vector 的内容。
根据cppreference.com ,operator[] 被重载:
reference operator[]( size_type pos );
const_reference operator[]( size_type pos ) const;
在什么情况下编译器会进行第二次重载?
【问题讨论】:
-
1.你可以用强制转换做很多危险的事情,2。修改通过 const 引用传递的东西本身不是问题。如果您实际上将 const 对象传递给此函数,这只会是一个问题。
-
如果传递一个空向量,您的代码也将是 UB
-
classic c cast 是一把大锤——你说什么都会做。看看 static_cast 会发生什么
-
基本上所有这些答案都需要很长一段时间才能说 C 风格的演员表将执行
const_cast的功能(除其他外)。
标签: c++ gcc vector reference constants