English 中文(简体)
Foo &foo = Bar()是合法的,还是汇编者。
原标题:Foo &foo = Bar() is legal or is it a compiler issue
struct Foo {};
struct Bar : Foo {};

Foo &foo = Bar(); // without const

载于对

最佳回答

您将这一提法定为临时价值。 之所以是临时性的,是因为Bar(>> ,就象一种功能,即:Bar,这种功能将在表述范围上和表述范围上蒸发,即行文末尾。 这种做法非常危险和非法,因为对<代码>foo的忽略造成了一种不明确的行为。 我没有想法,为什么MSVC++允许这种“名声”,而这种“名声”必须是一个坚实的错误。

定级4 (W4)

warning C4239: nonstandard extension used :  initializing  : conversion from  Bar  to  Bar & 

You should always write code with warning level 4.
Lastly, @KerrekSB s answer is better than mine, you should choose his as an answer.

问题回答

模板不包含非固定参考资料。 这只是措辞的设计方式。

虽然原则上不允许非经常接触温室的技术障碍,但这种限制几乎总是设计不好,而且往往在逻辑上说是不统一的,因此,标准只是允许的。 如果你认为有必要临时修改,那么你应当重新考虑你为什么要这样做。





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

热门标签