zer*_*s00 5 c++ memory pointers
一些简单的问题.
const int gFirst;
const int gSecond;
struct Data
{
static int First;
static int Second;
int first;
int second;
};
Data data;
Run Code Online (Sandbox Code Playgroud)
是否保证以下陈述是正确的?
&gFirst < &gSecond &Data::First < &Data::Second &data.first < &data.second1)该结果未指定.
2)此结果未指定.*
3)是.
标准中的相关部分是§5.9/ 2.指针之间关系的比较p,并q指定只在:
p并q指向同一个对象或函数,指向同一个数组末尾的一个,或者两者都为null.在这种情况下,p <= q并且p >= q是真的,p < q并且p > q是假的.p并q指向同一对象的非静态数据成员,指向后面声明的成员的指针比较大.(注意,这种比较不能在访问说明符之间.)p并q指向同一数组中的元素或超出数组末尾的元素,指向具有较高下标的元素或指向数组末尾的元素的指针比较大.p并q指向同一个union对象的数据成员,在这种情况下,它们比较相等.在所有其他情况下,未指定结果.
*因为它们是静态的,它们(显然)不会得到"非静态成员"规则.它们将在某些翻译单元中定义,因此就像任何其他指针一样.(未指定).
注意!有一种方法可以获得总排序,即通过std::less<void*>(以及所有其他比较函数对象).
这在§20.3.3/ 8中:
对于模板
greater,less,greater_equal,和less_equal,对于任何指针类型的专业化产生总订单,即使内置的运营商<,>,<=,>=没有.
所以,虽然你不知道是否std::less<void*>(&gFirst, &gSecond),true或者false你有保证:
std::less<void*>(&gFirst, &gSecond) ==
std::greater<void*>(&gSecond, &gFirst);
std::less<void*>(&Data::First, &Data::Second) ==
std::greater<void*>(&Data::Second, &Data::First);
Run Code Online (Sandbox Code Playgroud)
哪个可以证明有用.
答案:
1) Not guaranteed.
But probably.
But the order of initialization is guaranteed.
2) No.
You just have the declaration here.
You need to to define the instances in a source file.
Then they will behave the same way as the objects in (1).
3) Yes.
Run Code Online (Sandbox Code Playgroud)