English 中文(简体)
设置新的休息室和参考资料?
原标题:Placement new breaks consts and references?

Following the discussion on my answer to this question, apparently:

The following Code is 允许

struct Foo {
    int x;
};

Foo f;
Foo & f_ref = f;

(&f) -> ~Foo ();
new (&f) Foo ();

int x = f_ref .x;

but the following code is not allowed

struct Foo {
    const int & x;           // difference is const reference
    Foo (int & i) : x(i) {}
};

int i;
Foo f (i);
Foo & f_ref = f;

(&f) -> ~Foo ();
new (&f) Foo (i);

int x = f_ref .x;

Because of $3.8/7

如果在物体的寿命结束后,在所占领物体的储存被再利用或释放之前,在原物体所占领的储存地点制造了新的物体,指明原物体的点子,提及原始物体的参考资料,或原始物体的名称,将自动提及新物体,一旦新物体的寿命开始,即可用来操纵新物体,条件是:

  • the type of the original object is not const-qualified, and, if a class type, does not contain any non-static data member whose type is const-qualified or a reference type ...

我可以理解,如何提及<代码>f.x。 如果不复存在,则可能无效,但我看不出为什么f_ref。 纯粹因为其成员中某一成员是混淆和(或)参照的,而不应有:它是指<代码>。 Foo before and is a reference to a 之后的Foo

是否有人解释这一条件的理由?

Edit

感谢答复。 我不赞成“保证不作任何改动”的论点,因为我们don t目前允许选择异见者到切指,例如:

struct Foo {
    const int & x;
    Foo (const int & i) : x(i) {}
    void do_it ();
};

int i;
Foo f (i);
const int & ii = f.x;

f .do_it (); // may modify i
std :: cout << ii; // May NOT use cached i

I don t see how do_it is allowed to invalidate referenced values but operator new isn t -- Sequence points invalidate cached values: why should delete/placement-new be exempt?

最佳回答

我认为,其动机是允许汇编者在使用不为人所知的代码时,对<代码>const的物体(注: s constobjects,而不仅仅是指指指指指点人与参考和参考-const)和参考资料地址进行掩饰。

在你的第二个例子中,汇编者可以“首先看到”该物体已经制造和销毁,其次,该物体是用同样价值重造的。 但是,标准编辑的作者希望能够修改这一法典:

struct Foo {
    const int & x;
    Foo (int & i) : x(i) {}
};

int i = 1;
Foo f(i);

some_function_in_another_TU(&f);

std::cout << f.x;

为此:

struct Foo {
    const int & x;
    Foo (int & i) : x(i) {}
};

int i = 1;
Foo f(i);

some_function_in_another_TU(&f);

std::cout << i;           // this line is optimized

由于<代码>f的参考成员不能重新编号,因此must仍然指<代码>i。 拆除和施工作业违反了参考成员<代码>x<>/code>的不可变更性。

这种优化不应引起特别的争议:使用const的物体,而不是带有const的物体或参考成员:

const int i = 1;
some_function_in_another_TU(&i);
std::cout << i;

www.un.org/Depts/DGACM/index_french.htm 在其位置上,不能有效销毁该编码,并创建另一个具有不同价值的<条码>。 因此,应当允许汇编者在<代码>上发布:cout << 1;。 想法是,对其他类型的物体和参考物,也应作类比。

如果对身份不明的密码的呼声可以重新使用参考成员,或改变<条码>const数据成员的价值,那么将打破对语言的有用斜度(参比永远不会再重复,且物体永远不会改变其价值)。

问题回答

As far as I can tell, it s just a matter of semantic correctness, and the adherent assumptions that the optimizer may make. Consider this:

Bar important, relevant;

Foo x(important);  // binds as const-reference

Zoo z(x);  // also binds as const reference

do_stuff(z);

x.~Foo();
::new (&x) Foo(relevant);  // Ouch?

物体<代码>z可合理预计其<代码>。 Foo 成员指常数,因此指important。 如标准所述,过去两行的销毁加新建筑“自动更新所有提及(逻辑上)新物体的参考资料”,因此,尽管承诺不变,但“<代码>z

为了避免这种背弃义的侵权行为,整个重建工作都被禁止。

优化。 附录一

struct Foo
{
    int const x;
    Foo( int init ) : x( init ) {}
};

int
main()
{
    Foo a( 42 );
    std::cout << a.x << std::endl;
    new (&a) Foo( 3 );
    std::cout << a.x << std::endl;
    return 0;
}

The compiler, having seen a const int object, has the right to suppose that the value doesn t change; the optimizer might simply keep the value in a register accross the placement new, and output it again.

Note that your example is actually quite different. The data member has type int const&; it is a reference (and references are always const), so the compiler can assume that the reference always refers to the same object. (The value of this object may change, unless the object itself is also const.) The compiler can make no such assumption about the value of the object it refers to, however, since i (in your case) can clearly change. It is the fact that the reference itself (like all references) is immutable that causes the undefined behavior here, not the const that you ve written.

如果是符合资格的,你就不应修改。 延长寿命是一项具有严重后果的修改。 (例如,考虑主犯是否具有副作用)





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签