C++ 标准 [sec 5.7] 说:
The C++ standard [sec 5.7] says:
如果指针操作数和结果都指向同一个数组对象的元素,或者一个过去数组对象的最后一个元素,求值不应产生溢出;否则,行为是未定义.
If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.
那么,我是否正确地假设数组以外的其他类型的指针未定义?
So, am I correct in assuming that pointers one-past-the-end of other types than arrays are undefined?
例如:
int a = 0;
vector<int> v(&a, (&a)+1);
上面的代码片段编译并运行得很好(使用 g++),但它有效吗?
The above snippet compiles and works just fine (with g++), but is it valid?
不,这是合法的.5.7(4) - 您引用前的一段 - 说:就这些运算符而言,指向非数组对象的指针的行为与指向长度为 1 的数组的第一个元素,以对象的类型作为其元素类型."
No, it is legal. 5.7(4) - one paragraph before your quote - says: "For the purposes of these operators, a pointer to a nonarray object behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type."
这篇关于是“一过即尽"吗?非数组类型的指针是 C++ 中的有效概念吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
std::reference_wrapper 和简单指针的区别?Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和简单指针的区别?)
常量之间的区别.指针和引用?Difference between const. pointer and reference?(常量之间的区别.指针和引用?)
c++ - 如何从指向向量的指针访问向量的内容?How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何从指向向量的指针访问向量的内容?)
*& 的含义和**&在 C++ 中Meaning of *amp; and **amp; in C++(*amp; 的含义和**amp;在 C++ 中)
为什么我不能对普通变量进行多态?Why can#39;t I do polymorphism with normal variables?(为什么我不能对普通变量进行多态?)
取消引用已删除的指针总是会导致访问冲突?Dereferencing deleted pointers always result in an Access Violation?(取消引用已删除的指针总是会导致访问冲突?)