简体   繁体   中英

Which should I use: C99's complex.h library or the “GNU Extensions for Complex Number Types”?

I like the syntax of the "GNU Extensions for Complex Number Types" but I know that it isn't standard unlike complex.h. So, which should I use?

This is really a question of what's the most important to you.

If there's any chance that you may someday want to use the code on any compiler other than gnu, then it probably makes sense to use the standard "stuff" from <complex.h> .

If you're reasonably certain you're only targeting the gnu compiler, or at least consider the improvement in syntax they offer worth the possibility that you may eventually have to rewrite the code, then it may make more sense to use the gnu extensions instead.

At least to me it would come down to a fairly simple situation: if the code was (even reasonably) portable otherwise, I'd follow the standard for complex types as well. If the code uses enough gnu extensions otherwise that porting to another compiler will probably require a (nearly) complete rewrite, then you might as well use the gnu extensions if you like them better.

I should also add that porting isn't really the only consideration, but it all works together. Readability works pretty much the same way -- if you're using enough gnu extensions that somebody pretty much needs to know about gnu extensions to read the code, then using their extensions for complex won't be a bit loss. If the code is standard enough that somebody could otherwise read it, adding in proprietary extensions just for complex probably isn't a good idea.

Either one will cause portability issues. You'll have to decide based on what set of compilers you need your code to work with.

C99's <complex.h> is, of course, part of the standard language -- but not all compilers support C99. In particular, Microsoft has said that they're not planning to support C99 or any later standard, except for those features that happen to exist in C++. (C++ has complex number support, but as a library, not as a built-in set of types as C99 does.)

On the other hand, a gcc extension will only work with gcc, or with compilers that are sufficiently compatible with gcc.

I suspect that <complex.h> is going to be more portable than the gcc extension; you're not going to be able to use Microsoft's compiler with either one, but any implementation that supports C99 will support <complex.h> .

(Another alternative is to use C++ rather than C, and use C++'s <complex> .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM