English 中文(简体)
我正确地说,主编之后,对临时受约束的退约修改是oka吗?
原标题:Am I right in saying that const_cast followed by modification on a ref-to-const bound to a temporary is okay?

我要检查一下我对这个问题的理解和结论。


在挪威难民事务委员会上,有人要求:

可以接受<代码>const_cast a const。 提及受临时物体的约束?

<>Translating: 他有临时约束的对口,希望删除其const-ness加以修改。

我的答复是,我请a 此前的“号”问题类似,其中的共识似乎是,温室本身不是const,因此,你可以删除const-即你必须参考的编号,并通过结果加以修改。 只要原有的代号为<->const仍然存在,这便会影响到临时寿命。

也就是说:

int main()
{
   const int& x = int(3);

   int& y = const_cast<int&>(x);
   y = 4;

   cout << x;
}
// Output: 4
// ^ Legal and safe

什么时候?


<>(当然,不论这种守则是否实际可取,是另一个完全可取的问题!)

最佳回答

<>第号

First, as far as I can tell, whether it is a literal or not is irrelevant. Rvalues of non-class types always have non-cv qualified types (§3.10/9), however, in §8.5.3 (initialization of a reference), we have:

提及“cv1 T1”类时,先用“cv2 T2”类表述:

[......]

纽约总部

Otherwise, a temporary of type “cv1 T1” is created and initialized from the initializer expression using the rules for a non-reference copy initialization (8.5). The reference is then bound to the temporary. If T1 is reference-related to T2, cv1 must be the same cv-qualification as, or greater cvqualification than, cv2; otherwise, the program is ill-formed.

(以上各点均涉及价值或类别。)

就我们而言,我们有:

int const& x = ...;

So cv1 T1 is int const, and the temporary object we create has type int const. This is a top level const (on the object), so any attempt to modify it is undefined behavior.

这至少是我的解释。 我祝愿这一标准更加明确。

问题回答

答案取决于如何设立临时人员,以及这一提法是如何开始使用的。

如果你明确将临时代办作为非最基本的目标,而且情况保证,最优惠的人选将特别附在你创建的临时职位上,那么你就可以安全地消除参考的含意并修改标。

另一方面,如果该临时人员是由汇编者间接为你创造的,那么临时人员本身就会 con。 在该案中,修改这一临时办法后,可向联邦调查委员会提出。

不幸的是,C++语言标准本身似乎不能保证采用先入为主的方法的任何情况。 不管怎样,汇编员都可以介绍一份额外的临时原件。 新的临时人员(如上所述),因此是不可调和的。 如8.5.3/5所述,这种情况是否发生?

So, in general case the answer is no, while an implementation-specific answer might be different.





相关问题
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?

热门标签