6 Replies - 2363 Views - Last Post: 10 June 2012 - 03:25 PM

#1 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,157
  • Joined: 05-May 12

Why is string class incompletely declared by iostream header?

Posted 31 May 2012 - 02:15 PM

Why is the string class incompletely declared by the iostream header?

I have the follow code in VS2010:
#include <iostream>

int main()
{
    std::string a = "ABC";
    std::string b = "abc";
    bool equal = a == b;
    return 0;
}



Which gives an error on line 7:
error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)

So it wasn't too surprising that iostream somehow includes string. What was surprising was that I only got part of the string functionality.

I know that standard fix is to also #include <string> so that operator gets declared, but I'm trying to ask a deeper question: if iostream was dragging in the string class, why wouldn't it want bring in the string class and all its other associated documented friends by including the string header? Is this just a poor implementation decision by Microsoft or is there a real gain to having string declared partially when iostream is included?

(Doing a quick check with GCC 3.4.4, it looks like it drags in the complete set of declarations for string because I don't get any compile errors.)

Is This A Good Question/Topic? 0
  • +

Replies To: Why is string class incompletely declared by iostream header?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,674
  • Joined: 25-December 09

Re: Why is string class incompletely declared by iostream header?

Posted 31 May 2012 - 02:40 PM

Quote

if iostream was dragging in the string class, why wouldn't it want bring in the string class and all its other associated documented friends by including the string header?

Since their implementation doesn't require all the friends and such in iostream it actually makes sense not to automatically include all the string classes, in my opinion. The gain would be possibly faster compiles and a smaller faster executable. It is also considered bad practice to include files you don't require or assume that some other file will include a required header for you. If you use a function, class that is defined in a header file you should always include that file yourself.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,157
  • Joined: 05-May 12

Re: Why is string class incompletely declared by iostream header?

Posted 31 May 2012 - 03:35 PM

View Postjimblumberg, on 31 May 2012 - 02:40 PM, said:

If you use a function, class that is defined in a header file you should always include that file yourself.


So if in the course of Windows programming I use STDAPI, which is declared by winnt.h, but included by windows.h, I should not only include windows.h, but winnt.h as well?

Just playing Devil's advocate. :lol: I do get your point.

I guess that it was just my surprise that the string declarations worked, but the operators did not. It's like discovering in your code that you can use CHAR_MAX, but somehow UCHAR_MAX wasn't defined.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Why is string class incompletely declared by iostream header?

Posted 31 May 2012 - 05:34 PM

> So if in the course of Windows programming I use STDAPI, which is declared by winnt.h, but included by windows.h, I should not only include windows.h, but winnt.h as well?

Not in this case. Because Windows.h is used to properly include a number of the various individual windows headers, which all have dependencies. That's why you see statements like: " Header: Winnt.h (include Windows.h) " (from MSDN). You're being expressly told to include windows.h

> It's like discovering in your code that you can use CHAR_MAX, but somehow UCHAR_MAX wasn't defined.

If you didn't the limits header, you would have no reasonable expectation of either constant, and so if one "worked", it's your fault for relying on it.

I don't think you have a leg to stand on at all here.
Was This Post Helpful? 2
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,157
  • Joined: 05-May 12

Re: Why is string class incompletely declared by iostream header?

Posted 31 May 2012 - 11:24 PM

Thanks guys!
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: Why is string class incompletely declared by iostream header?

Posted 10 June 2012 - 02:31 PM

I don't know if you have ever used the iosfwd header which includes just enough code so you can use pointers/references to both ostreams and istreams and probably a few other things. The iostream header includes a lot so if you just need a pointer or reference to a stream in your own header file, it might be better to just include iosfwd in the header, and then include whatever headers you need to implement your header in the .cpp file.

I am using g++ 4.7 and in the implementation of the C++ standard library, there is a header file in the "bits" folder called stringfwd. Well in the iostream header, both the istream and ostream headers are included. They both include the ios header which in turn includes the iosfwd header. The iosfwd header includes that stringfwd header. That's probably why some of the functionality of strings is missing.
Was This Post Helpful? 0
  • +
  • -

#7 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Why is string class incompletely declared by iostream header?

Posted 10 June 2012 - 03:25 PM

it's the pimpl idiom
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1