Live data from Hacker News

Why artificially limit your code to C?

stackoverflow.com

11–20 of 40 posts

Re: Why artificially limit your code to C?

#11

No one there mentions symbol scrambling, and how it makes C++ libraries unportable across multiple compilers for the same platform.

Actually this is mentioned by "Rhythmic Fistman", but the post only got 6 points.

From a purely practical standpoint, the lack of an ABI is the #1 reason I don't bother with C++ anymore because chances are pretty good that I'll want to interface the lower-level code with something like Python or Go, both of which integrate with C code much more easily than C++.

Re: Why artificially limit your code to C?

#12
post #7

No one there mentions symbol scrambling, and how it makes C++ libraries unportable across multiple compilers for the same platform.

Everyone loves to bash the lack of a C++ ABI. Now please name another language with native implementations with a standard ABI across different compilers. C implementations have a standard ABI, because they usually are the operating system ABI.

We love to bash it because it's shit.

Also, to answer your question: Java, C#/Mono.

Re: Why artificially limit your code to C?

#13

> C is small and simple and I can fit the whole language in my brain Except when you see Duff's device for the first time and realize that C you thought you knew what's the actual C :) http://catb.org/jargon/html/D/Duffs-device.html

Go one step further and use Duff's Device to implement coroutines! http://webcache.googleusercontent.com/search?q=cache:pA85c1U...

Re: Why artificially limit your code to C?

#14
The fact that C++ is almost a superset of C is commonly used as an argument for C++, and the accepted answer in the post is a common response - C++ is not quite a superset of C, you have to cast malloc, you lose out on C99 features, etc. But I think it misses the point: if you could really get the advantages of C++ by using its features in a few necessary places in your C code, I think it would be worth it to make your code slightly uglier to accommodate it.

You can't.

Generic containers are probably the C++ feature C is most sorely lacking. But as soon as you start using map in your code, you have to deal with slower compilation in all the files that use it (if a map is declared in a header file, e.g. as a struct member, that could be quite a lot), repetitive code generation the linker might or might not optimize, verbose syntax (std::map::iterator, yay), and the requirement (more or less) to use new instead of malloc for any struct that contains a C++ object. And if you want your map's performance to not suck, you'll need to depend on either a nonstandard extension or C++11 to get hash_map/unordered_map.

Then there's writing templates - even in the context of C++, template code tends to be hideously ugly (template everywhere, the aforementioned issues with code generation and sticking everything in header files, pages of confusing error messages when you mess up), and is hard to debug with a debugger. Trying to use them with C can only lead to pain - especially since they work best when you have actual objects to specialize on. Templates do allow certain C code to be written more nicely, such as binary formats where there are multiple versions of the same struct, but even then there are limitations to the magic - template-ness tends to spread to callers.

gcc apparently wanted to switch to C++ partly in order to use destructors: now you have spooky action at a distance, where the caller of your method is a closing brace, thanks to a declaration a page away somewhere up in the function.

Do you want to use a few classes to add some structure to your code? It will certainly start to seem a bit forced to declare struct foo and functions like foo_alloc and foo_get_bar once you're in C++, when declaring a class expresses the concept more elegantly. And then you're stuck with C++'s context sensitive nature (anyone who understands the code will know that it->getName() is a reference to Foo::getName, and fooCount used here is a member variable, so it's fine, right?) and poor separation of interface and implementation (it really doesn't make sense that you have to declare private fields in your header file, and it doesn't make compilation any better), and if you're not careful your code will start to feature a vector of objects where an array of ints is appropriate, or yet another method whose only purpose is to pass on slightly different arguments to a method of a member variable.

C++ programmers accept these issues in exchange for a high-level coding style (less security bugs!) and an easier time writing clean code than C programmers (although bad C++ code is a whole lot worse than bad C code). You will gain hardly any of that by sprinkling C++ into C. Solving these problems in C sometimes leads to more work and more verbose code, but the result is also often a better solution. If you absolutely must have code generation, use a macro, they're not as bad as C++ programmers make them out to be - or, at least, they're ugly enough that most programmers know to use them very sparingly. (I'm rather a fan of , for example, although you do have to understand how it works to make sense of code that uses it.)

Re: Why artificially limit your code to C?

#15
post #14

The fact that C++ is almost a superset of C is commonly used as an argument for C++, and the accepted answer in the post is a common response - C++ is not quite a superset of C, you have to cast malloc, you lose out on C99 features, etc. But I think it misses the point: if you could really get the advantages of C++ by using its features in a few necessary places in your C code, I think it would be worth it to make yo…

Really good article. edit: oops, down points don't agree.

Re: Why artificially limit your code to C?

#16
C++ is not a superset of C, therefore using C as C++ needs porting. It's fine if you consciously restrict yourself to C++-compatible subset of C (GCC warning -Wc++-compat can help you), but if you don't it can take a significant effort.

For a realistic case of compiling large C codebase not written in C++-compatible subset of C as C++, take a look at GCC. I think it took something like 6 months to compile GCC as C++ regression-free.

http://gcc.gnu.org/wiki/gcc-in-cxx

Re: Why artificially limit your code to C?

#17
post #5

No one there mentions symbol scrambling, and how it makes C++ libraries unportable across multiple compilers for the same platform.

That's what I thought too, until I found that there exists C++ ABI on linux. I tried to link together two small files, both using iostreams, one compiled with Intel C++, the other with g++, and it worked. Though I'm in doubt [haven't tested it yet] whether it would have worked had I tried to use something std::vector across functions compiled with different compilers.

clang uses the same ABI as gcc and if you find that something is binary incompatible then it is a bug.

Re: Why artificially limit your code to C?

#18
post #14

The fact that C++ is almost a superset of C is commonly used as an argument for C++, and the accepted answer in the post is a common response - C++ is not quite a superset of C, you have to cast malloc, you lose out on C99 features, etc. But I think it misses the point: if you could really get the advantages of C++ by using its features in a few necessary places in your C code, I think it would be worth it to make yo…

> where the caller of your method is a closing brace

Although I agree with the sentiment of your comment destructors and RAII in general are arguably the best C++ features.

If I were forced to use C++ again I would restrict my code to a lightweight subset of C++, especially I would not use the C++ Standard library (STL, iostreams, string) and of course not use BOOST (or any template heavy library) and C++0x. But I would definitely include 'full-fledged' exception handling from the start.

Re: Why artificially limit your code to C?

#19
post #13

> C is small and simple and I can fit the whole language in my brain Except when you see Duff's device for the first time and realize that C you thought you knew what's the actual C :) http://catb.org/jargon/html/D/Duffs-device.html

Go one step further and use Duff's Device to implement coroutines! http://webcache.googleusercontent.com/search?q=cache:pA85c1U...

Dear HN: Please give me more upvotes per comment Signed, people who are truly impressed by this absurd abuse of C

Re: Why artificially limit your code to C?

#20
post #14

The fact that C++ is almost a superset of C is commonly used as an argument for C++, and the accepted answer in the post is a common response - C++ is not quite a superset of C, you have to cast malloc, you lose out on C99 features, etc. But I think it misses the point: if you could really get the advantages of C++ by using its features in a few necessary places in your C code, I think it would be worth it to make yo…

> where the caller of your method is a closing brace Although I agree with the sentiment of your comment destructors and RAII in general are arguably the best C++ features. If I were forced to use C++ again I would restrict my code to a lightweight subset of C++, especially I would not use the C++ Standard library (STL, iostreams, string) and of course not use BOOST (or any template heavy library) and C++0x. But I wo…

What would you use for string handling, then? Please don't tell me you prefer strcpy/strcat and zero-terminated strings?
Post reply on HN