質問

C and C++ have standards, but support isn't perfect, the only available copies on the internet are drafts, and there are immensely useful things that aren't standard, such as __attribute__((cleanup)). Apparently the glib authors feel that's sufficiently ubiquitous even though it's a GCC extension.

Then there are other GCC extensions (such as empty __VA_ARGS__ in macros and the ability to token-paste away superfluous commas when __VA_ARGS__ is empty) which clang has decided should throw a warning.

In the web development world, there's caniuse, which tells you specifically which versions of each browser you're supporting if you use a particular language feature. Is there any equivalent service for C++ compilers, that would give me a quick way to search for e.g. __attribute__((cleanup)) and get a list of compilers that support it?

役に立ちましたか?

解決

You decide which compilers you want to use or support. The available non-standard features follows directly from this choice of compilers. Some common software such as glibc, Linux, and GCC itself are primarily intended to be compiled with GCC, though Clang has good GCC compatibility.

Similarly, you decide which C or C++ language standard you are targeting.

Keeping broad compatibility among compilers is important when distributing software as source code, in order to be compiled by users themselves. Portability can be achieved by adhering to standards, in particular the language specification and (for portability among Unix-like systems) POSIX.

Anecdotes: Once upon a time I published source code conforming to C++14, but found that it was possible to easily support a much wider range of compilers by sticking to a subset of C++11 that happened to be available in old GCC versions. In a different project, the target compiler was determined by the client, but it was possible to use extensions. But whereas the compiler was known the target operating system had to remain portable, limiting availability of certain features.

Note that while there is no “caniuse” style site for compiler features, the compilers do have documentation about which standardized features they support. E.g. GCC has their cxx-status page, as has Clang. For non-standard features, the compiler's documentation on language extensions has to be consulted. To some degree, it is also possible to have the preprocessor check for the existence of extensions or language features, e.g. with __has_attribute. Where no direct checks are possible, the classic solution is to write a configure script that tests for the presence of features through example programs.

ライセンス: CC-BY-SA帰属
所属していません softwareengineering.stackexchange
scroll top