12

With Mingw 4.7.2, I have a library that doesn't compile because of a call to isnan. The compiler says "everything will be fine" if I use std::isnan, and indeed I manage to compile my file.

But if I check here (Edit: but maybe I should have checked also here :-) ), the std:: doesn't seem to be necessary. If I add it, will the file be portable?

More in general, for each case is there a general way to understand when putting std:: is necessary (for portability), optional or to be avoided?

Edit

Indeed among the origins of the problem is that there are multiple header inclusions, and some of the included headers include <cmath>, while this cpp file tries to include <math.h> (when <cmath> has already been included).

3
  • 1
    Don't check there, check here. Commented Aug 8, 2013 at 14:51
  • @ChristianRau So basically in C++98 this did not exist, and one could have it only by borrowing it from C! That's interesting!
    – Antonio
    Commented Aug 8, 2013 at 14:55
  • No, it has always been the way it is. That link merely should show you that there is a big std:: in front of the function name. By coincidence that particular function isnan is only supported with C++11 and didn't exist in C++98 at all (not even in the C header, which was the C89/90 header and not the C99 header that C++11 uses). So in one way you could borrow it from C, but not from the C included with C++, but from an actual C99 (though that's ought to be a strange mix then). Commented Aug 8, 2013 at 14:57

3 Answers 3

19

It depends on which header you include. If you include the C header <math.h> (which is part of C++, albeit marked as deprecated), then you can use the unqualified C functions, like isnan. If you on the other hand include the C++ header <cmath>, you are only guaranteed that it brings all the functions from <math.h> into the std namespace and thus you have to properly qualify them, like std::isnan (or use some kind of using directive). Unfortunately an implementation is allowed but not required to bring those functions into the global namespace, too, when including <cmath> (and thus it is one of the many "works on my machine"-incidences of C++ and the reason why many people write code like you just tried to compile unsuccessfully).

So to sum up: Either include <math.h> and use isnan or include <cmath> and use std::isnan, everything else is non-portable. Of course all this applies to any other C header and its respective C++ version, too.

EDIT: It should be noted though, that this particular function isnan is only supported since C++11 and wasn't available in C++98 at all (which may be part of your confusion). But this doesn't change anything in this situation because in C++98 neither <cmath> nor <math.h> (which was the actual C89/C90 header back then and not the C99 header that C++11 includes) had this function, since they're always in-sync. So what this library from your question maybe tried was to use C++98 while taking the isnan function from a different C99 implementation (which isn't a particularly good idea, as it might conflict with the C89/C90 parts of the C++ implementation, never even tried this though).

6
  • So <math.h> is deprecated! In C++98 one should use only <cmath> then? And to have code that works both in C++98 and C++11 would be possible?
    – Antonio
    Commented Aug 8, 2013 at 14:57
  • @Antonio This whole problem doesn't have anything to do with C++98 vs C++11. It has always been like described in my answer, no matter what C++ standard. And yeah, the C headers are called deprecated, but in practice that doesn't really mean much and it is rather a matter of taste which one you use (I for myself, being quite an anti-C snob and a big fan of spamming std:: all over the place, prefer the C++ headers, though). But you should stay consistent of course, otherwise you happen to experience such stangeties like you actually did. Commented Aug 8, 2013 at 15:00
  • [this was written before your comment] Looking at Christian Rau comment/link above: isnan of <cmath> shouldn't even be there in C++98... And the only isnan for C++98 would be in the deprecated <math.h>, which must be called without std::. So to be portable is... impossible :) unless putting some cumbersome conditional compiling.
    – Antonio
    Commented Aug 8, 2013 at 15:09
  • Regarding your comment: so is <cmath> what you call a C or C++ header?
    – Antonio
    Commented Aug 8, 2013 at 15:11
  • 1
    @Antonio Like written in the answer <cmath> is the C++ header, while <math.h> is the C header included with the C++ library. Commented Aug 8, 2013 at 15:13
5

C has no notion of namespaces. When you write #include <math.h> all the names declared in the header go into the global namespace, and you need to write isnan.

C++ has namespaces. Still, when you write #include <math.h> all the names declared in the header go into the global namespace, and you need to write isnan, just like in C.

In addition, when you write #include <cmath> all the names declared in the header go into the namespace std, and you need to write std::isnan.

Further, C++ implementations are allowed to also go the other way, with #include <math.h> putting the names into std as well as in the global namespace, and with #include <cmath> putting the names into the global namespace as well as in std. Don't rely on this; code that does so is not portable. This is a concession to implementors to make things easier; what it really means is that if you use #include <cmath> you cannot assume that there will be no isnan in the global namespace and that if you use #include <math.h> you cannot assume that there will be no isnan in std.

4
  • So what happens if I include both? (Looking at this specific example, I am going to have inconsistent behaviour)
    – Antonio
    Commented Aug 8, 2013 at 15:19
  • 2
    @Antonio That probably depends on their repective include order and their include guards. In short, don't ever do this. Commented Aug 8, 2013 at 15:22
  • @ChristianRau Thanks, I just needed an authoritative answer :) (I am not the writer of the library)
    – Antonio
    Commented Aug 8, 2013 at 15:24
  • @Antonio - if you include both you are in a state of sin. Which doesn't mean that it won't work: standard headers have to do the right thing regardless of which other standard headers are also used. So if you include both you get names in both std and the global namespace. Commented Aug 8, 2013 at 16:22
3

That's because isnan is from C. Using different type of include will lead to different results. Take isnan from C header <math.h> as an example:

If you use #include <cmath>, it will be put in the std namespace.

If you use #include <math.h>, it will be put in the global namespace.

C++11 D.5 C standard library headers

Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).

[ Example: The header assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.