Visual C 的示例不合格的代码?
使用 Visual C++ 时有哪些不符合标准的代码示例?允许在 Visual C++ 下编译的东西,但没有其他东西。
What are some examples of code that are not standards compliant when using visual C++? Something that is allowed to compile under visual C++ but nothing else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以在此处找到所有 Microsoft 语言扩展;您可能还想了解一下语言领域,其中VC++ 不符合标准。
我认为是标准的(当我启用 /Za switch) 是“神奇的左值转换”:
You can find all the Microsoft language extensions here; you may also want to have a look to the areas of the language where VC++ is not compliant to the standard.
One that I thought was standard (I noticed it when I enabled the /Za switch) is the "magic l-value cast":
Visual C++ 的某些版本接受将非常量引用传递给临时对象。像这样的东西:
Some version of Visual C++ accept to pass non-const reference to temporary object. Something like that :
和
并不描述完全相同的事情。编译器可能完全兼容标准,同时具有该编译器独有的扩展,而不兼容是标准明确禁止的事情。还有许多“未定义”或ISO 标准的“实现定义”部分可能会阻止可移植性而不不兼容。此外,许多受支持的扩展都受到其他编译器的支持,因此这是您的约束之一的示例,但不是另一个约束
。导致其代码不可移植的 VC++ 扩展是所有 C++/CLI 扩展,因此也是需要它们的 .NET Framework 类库。
and
do not describe exactly the same thing. A compiler may be fully standard compliant while having extensions that are unique to that compiler, while a non-compliance is something explicitly prohibited by the standard. There is also a number of "undefined" or "implementation defined" parts of the ISO standard that might prevent portability without being non-compliant. Moreover many supported extensions are supported by other compilers so are examples of one of your constraints but not the other.
Now that said, the major extension of VC++ that would render its code non-portable are all of the C++/CLI extensions, and therefore also the .NET Framework class library which requires them.
microsoft.com 上有一个 官方页面 说明了哪些部分VC++ 与标准不兼容。然而,另一个问题是它默认与标准兼容。例如,
for
变量的默认范围是仍然错误VC++2010。There is an official page on microsoft.com saying what are the parts where VC++ is not compatible with the standard. However a different issue is where it is compatible by default with the standard. For example the default scope for
for
variables is still wrong in VC++2010.我没有 VC 编译器来测试这个,但如果我没记错的话,无论注释的错误如何,它都可以在 Visual Studio 中正常编译:
I don't have a VC compiler to test this, but if I recall correctly this will compile fine in Visual Studio regardless of the commented errors:
MSVC++ 允许您做的一件事是在类中显式地专门化模板。例如。
这在 VS 中编译得很好,但尝试在 GCC 中编译会给出错误,告诉您在非命名空间范围中有显式专业化。解决办法就是简单地把专业化拉长。
不过,根据规范,GCC 在这个问题上是正确的,规范规定所有显式专业化都应该在命名空间范围内。
可能值得注意的是,在后一种情况下,您必须在头文件中定义您的专业化,而不是像您通常期望的那样在实现文件中定义。提到的两个编译器都不符合解决此问题的标准,即在实现文件中的专门化上声明的
export
关键字。不过,大多数编译器并未实现此功能,并且计划从下一版本的规范中删除它。One thing MSVC++ allows you to do is to explicitly specialize templates within a class. Eg.
This compiles fine in VS, but attempting to compile in GCC will give an error telling you that you have an explicit specialization in the non-namespace scope. The solution to which is to simply drag out the specialization.
GCC is correct on this issue though, according to the spec, which states that all explicit specializations should be in the namespace scope.
Possibly worth noting that in the latter case, you must define your specialization within the header file, and not the implementation file like you would normally expect. Both the compilers mentioned are non compliant with the standard which would solve this problem, which is the
export
keyword declared on the specialization within an implementation file. This feature is not implemented by most compilers though, and there are plans to remove it from the next version of the specification.