Wednesday 16 November 2011

Should C++ additions to a compiler be used in a C program? in C programming

Should C++ additions to a compiler be used in a C program?

Not unless your “C program” is really a C++ program.

Some features of C++ were so nifty that they were accepted by the ANSI/ISO C standards committees. They’re no longer “C++ additions”; they’re part of C. Function prototypes and the const keyword were added to C because they were really good ideas.

A few features of C++, such as inline functions and ways of using const to replace #define, are sometimes called “better C” features. There have been a few partly C++ compilers with a few of these features. Should you use them?

Here’s one programmer’s opinion: If you want to write C code, write C code. Write code that all C compilers will accept. If you want to take advantage of C++ features, move to C++. You can take baby steps, a few new tricks at a time, or you can go all out and create templated pure abstract base classes with lots of inlines and exceptions and conversion operators. After you’ve crossed the line, though, you’ve crossed it; your program is now a C++ program, and you shouldn’t expect a C compiler to accept it. Now let me say this. Work has started on a new C standard, one that will include some C++ features and some brand new features. Over the next few years, some of those new features will be implemented by some compiler vendors. That doesn’t guarantee they will be implemented by all compilers, or make it into the next C standard. Keep your ears open. When it sounds as if a new feature has really caught on, not just in the compiler you use but in all the ones you might use, then think about using it yourself. It didn’t make sense to wait until 1989 to start using function prototypes. On the other hand, it turns out there was no good time to start using the noalias keyword if you wanted your code to be portable.

Cross Reference:

XV.2. What is the difference between C++ and C?

No comments:

Post a Comment