|
-
July 1st, 2008, 04:52 PM
#1
A newbie question... importance of IOstream?
I'm a physics graduate student, so I'm implementing some relatively mathematical stuff in visual studio at the moment, and I was writing a class that would act like a very particular kind of array. This class didn't need to print out anything or take in any input from the user, so I didn't think to include the IO stream library. I did however include math.h because it had a function I needed, min. So everything's fine but then the compiler complains it doesn't knwo what min is, even though it found the library math.h. Next I try and add using namespace std; in the source code, in the header, it just spits back errors. Finally, quite by accident I include iostream, and all of a sudden everything works, I can put the using namespace std wherever I want, or just use std::min... I don't understand this at all. I thought iostream was just one particular library with functions for inputting and output? Why did i have to include this library to get namespace std, and ultimately the min function to work?
-
July 1st, 2008, 04:57 PM
#2
Re: A newbie question... importance of IOstream?
There is no min function in math.h. There is std::min and std::max in the <algorithm> header, which <iostream> may include itself on some platforms.
Still, it's best to *never* use min() and max() as such. Microsoft defines these as macros (one of their more boneheaded decisions), so you're likely to get compiler errors even using the functions in namespace std.
Instead, define your own functions mymin() and mymax(). They're pretty simple so there's no reason not to.
-
July 1st, 2008, 06:15 PM
#3
Re: A newbie question... importance of IOstream?
or you could define NOMINMAX to disable the stinking microsoft macros.
you may run into problems with third party code using the stinking macros in header files.
I hate those macros.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
July 1st, 2008, 09:06 PM
#4
Re: A newbie question... importance of IOstream?
Note that stdlib.h defines min and max macros which are *not* disabled by NOMINMAX, but only for C code, not C++. At least on VS 2005.
-
July 2nd, 2008, 01:37 AM
#5
Re: A newbie question... importance of IOstream?
True, fair enough it was in algorithm, however regardless why would using namespace std throw an error if not used in conjunction with #include <iostream> ? Also, why is it bad that they are macros? I thought that from a pure speed perspective macros tend to be faster than function calls, which is all I really care about in this context. Thanks for your responses thus far guys, really appreciate it.
-
July 2nd, 2008, 02:22 AM
#6
Re: A newbie question... importance of IOstream?
most likely you needed to include some part of the standard library to get namespace std.
1. You should never put using namespace XXX in a header file. Doing so completely removes the purpose of the namespace.
2. Macros have nothing to do with speed. They are used by the linker for text replacement. You are thinking of inlining functions, which is an optimization the compiler may or may not implement. The function implementation needs to be visible in the translation unit for the compiler to inline a function call. the standard library implementation of min/max are template functions and allow for this compiler optimization.
3. Macros subvert the C++ type system and do not respect namespaces.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|