C++有什么特点?与 Visual Studio 以外的编译器不兼容?
我被告知要避免使用此类 C++ 功能,因为这使得将代码移植到其他编译器变得困难。
我给出的示例是在头文件中使用 #ifdef
而不是 #pragma Once
。
I was told to avoid using features of C++ like these as it makes it difficult to port the code to other compilers.
The example I was given was using #ifdef
instead of #pragma once
in my header files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,这个问题很难回答,因为每个编译器都是不同的,更具体地说,#pragma 语句不是 C++ 的功能。根据定义,#pragma 的意思是“发送给编译器的命令”:
“根据定义,编译指示是特定于机器或操作系统的,并且通常对于每个编译器来说都是不同的。” MSDN
因此,基本上,每当您看到 # pragma,它的意思是“接下来的内容不是语言标准的一部分,因此对于您定位/编译的每个平台可能会有所不同”
Well, this is challenging to answer, because each compiler is different - and, more specifically, #pragma statements are not a feature of C++. #pragma means, by definition "a command to send to the compiler":
"Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler." MSDN
so, essentially, whenever you see #pragma, it means "what follows next is not part of the language standard, and so may be different for every platform you target/compile on"
这些不是“C++ 特性”,而是编译器开发人员提供的非标准“扩展”、非标准函数和“编译器特性”。
简短且不完整的 Microsoft 特定功能列表,这些功能会在移植过程中引起问题:
pragma Once。 (以及几乎每个编译指示)将被另一个编译器忽略,这将导致多个标头包含。可能会引起麻烦。
Those are not "C++ features", they are non-standard "extensions", non-standard functions, and "compiler features" provided by compiler developer.
short and incomplete list of microsoft-specific features that will cause trouble during porting:
pragma once. (and pretty much every pragma) Will be ignored by another compiler, which will result in multiple header inclusions. Can cause trouble.
以下是 VC++ 中的非标准行为列表: http:// /msdn.microsoft.com/en-us/library/x84h5b78%28VS.71%29.aspx
Here's a list of nonstandard behaviour in VC++: http://msdn.microsoft.com/en-us/library/x84h5b78%28VS.71%29.aspx
非常干净,但不可移植的 for every, in 语句:Visual C++“对于每个”可移植性
The very clean, but non-portable for each, in statement: Visual c++ "for each" portability