3

If one actually wants functions placed into the global namespace, then is there any benefit to using this style:

#include <cfoo>
using namespace std;

Or can one just do

#include <foo.h>

for an identical effect?

cdhowie
  • 158,093
  • 24
  • 286
  • 300
fieldtensor
  • 3,972
  • 4
  • 27
  • 43

1 Answers1

2

If "foo.h" is not "properly prepared" to be compiled with C++, it will not work out at all well in a C++ environment.

The "cfoo" style headers are there to make the contents "suitable" for C++. The fact that the old style headers do work is mainly to maintain compatibility.

Both solutions are quite equally bad in the sense that when you later on discover that you have just got a name-clash, so all calls to foo needs to be updated to use either bleh::foo or std::foo to make sure that you know which one is meant.

But at least if you have using namespace std, you have something to search for. If you want to find all ".h" files, you'll probably find your own .h file, some "ncurses.h" and similar that are not what you where looking for.

So, in conclusion, neither are good solutions, but

#include <cfoo>; 
using namespace std;

is a bit less bad.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I'm curious to know what exactly "suitable" means? Mainly, is it just more stylistically "suitable," or are there real technical differences that I might actually care about? – fieldtensor Aug 22 '13 at 22:50
  • It's a technical thing: C++ "mangles" function names (to allow functions with the same name that takes different types of parameters). So if you have a `int Foo(int x, double y);` the actual name may be `_iFoo_iD`. C doesn't do this. So C-style header files that are to be included by C++ compiler needs to have `extern "C" {` and `}` around any functions that are `C` to "turn of mangling". If you don't do that, you'll get a bunch of "Symbol _iFoo_iD not found in x.cpp" errors. – Mats Petersson Aug 22 '13 at 23:01
  • I believe that both `#include ` and `#include ` are required to work correctly for a C++ program. The difference is that `` puts declarations in the global namespace (and may also optionally put them under `std::`), while `` puts declarations under `std::` (and may also optionally put them in the global namespace). – Keith Thompson Aug 22 '13 at 23:11
  • I'm starting to get the impression that for my purposes they're really identical, and that I might as well use the .h headers. Interesting. – fieldtensor Aug 22 '13 at 23:12
  • By the way, the reason I would be prone to using the .h headers is because I'm programming for UNIX, and I'm using a lot of the POSIX API that isn't present in the std:: namespace. So, at the moment my code has things like std::memcpy() in some places, but just plain strerror_r() in other places. Or std::realloc() in some places, but just plain write() in other places. I'm wanting to unify it, and since I can't get things like strerror_r() or write() into std:: I figure that I'll just take the global non-std:: versions of everything for the sake of uniformity in all of my C API usage. – fieldtensor Aug 22 '13 at 23:17
  • No, the POSIX api will never be in the `std` namespace. However, you should really isolate any API-specific code anyway, so there shouldn't be much of a "mixing" of API-calling code and regular C++ style code. – Mats Petersson Aug 22 '13 at 23:21
  • And if your C++ code is calling `realloc`, you are doing it wrong... – Mats Petersson Aug 22 '13 at 23:23
  • My comment above is basically what [this answer](http://stackoverflow.com/a/10460277/827263) to the duplicate question says. – Keith Thompson Aug 22 '13 at 23:41