C++有什么特点?与 Visual Studio 以外的编译器不兼容?

发布于 2024-09-07 14:37:01 字数 122 浏览 1 评论 0原文

我被告知要避免使用此类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娜些时光,永不杰束 2024-09-14 14:37:01

嗯,这个问题很难回答,因为每个编译器都是不同的,更具体地说,#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"

燃情 2024-09-14 14:37:01

这些不是“C++ 特性”,而是编译器开发人员提供的非标准“扩展”、非标准函数和“编译器特性”。

简短且不完整的 Microsoft 特定功能列表,这些功能会在移植过程中引起问题:

  1. pragma Once。 (以及几乎每个编译指示)将被另一个编译器忽略,这将导致多个标头包含。可能会引起麻烦。

  2. __int32 和类似类型(特定于 Microsoft)
  3. 来自 windows.h 的所有内容 - DWORD/WORD/HANDLE/TCHAR。还有特定于操作系统的 API 和系统调用。这包括 WinMain()。
  4. 每个以两个下划线开头的内置类型、宏和关键字(____FUNCTION____、__int32、__declspec 等)。
  5. *printf 函数的某些版本 - swprintf、vswprintf 等。某些格式 (%S) 规范在不同编译器上的行为有所不同。
  6. *_s 函数(strcpy_s 等)。

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:

  1. pragma once. (and pretty much every pragma) Will be ignored by another compiler, which will result in multiple header inclusions. Can cause trouble.

  2. __int32 and similar types (microsoft specific)
  3. Everything that comes from windows.h - DWORD/WORD/HANDLE/TCHAR. Also os-specific API and system calls. This includes WinMain().
  4. every builtin type, macros and keyword that starts with two underscores (____FUNCTION____, __int32, __declspec, etc).
  5. Certain versionf of *printf functions - swprintf, vswprintf, etc. Some format (%S) specifications behave differently on different compilers.
  6. *_s functions (strcpy_s, etc).
撩人痒 2024-09-14 14:37:01

以下是 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

只怪假的太真实 2024-09-14 14:37:01

非常干净,但不可移植的 for every, in 语句:Visual C++“对于每个”可移植性

The very clean, but non-portable for each, in statement: Visual c++ "for each" portability

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文