when to include <string>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Travis

    when to include <string>

    So here's something I've always wondered. Typically if I want to use a
    string I simply

    using namespace std::string;

    string myString;

    But when do I actually need to include <string>. What does that
    provide me and why, for some uses, do I not need it?

    Thanks.
  • red floyd

    #2
    Re: when to include &lt;string&g t;

    Victor Bazarov wrote:
    Travis wrote:
    >So here's something I've always wondered. Typically if I want to use a
    >string I simply
    >>
    >using namespace std::string;
    Try either

    using namespace std;

    or

    using std::string;

    but not the original text. So you'd have:

    #include <string>
    using namespace std;

    or

    #include <string>
    using std::string;
    >string myString;
    >>
    >But when do I actually need to include <string>. What does that
    >provide me and why, for some uses, do I not need it?
    >
    You need to #include <stringwhenev er you use std::string. Either
    explicitly as std::string, or via a using clause.

    Comment

    • Andrey Tarasevich

      #3
      Re: when to include &lt;string&g t;

      Travis wrote:
      So here's something I've always wondered. Typically if I want to use a
      string I simply
      >
      using namespace std::string;
      This will not even compile. You probably meant

      using std::string;
      string myString;
      >
      But when do I actually need to include <string>. What does that
      provide me and why, for some uses, do I not need it?
      ...
      If you want to use 'std::string', you have to include <stringdirect ly
      or indirectly. The only reason you were able to get away without
      including it yourself is that you must've included some other header
      file, which included <stringinternal ly. For example, in some
      implementation of standard library header <iostreammigh t include
      header <stringinternal ly, so when you include <iostreamyou
      automatically include <stringwithou t even noticing it. But note, that
      it is not guaranteed to always be that way. In other words, you code
      compiled because you've been lucky so far. If you want your code to be
      portable between platforms and compiler/library versions, be sure to
      include <string(directl y or indirectly) into every translation unit
      that uses 'std::string'.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • dave_mikesell@fastmail.fm

        #4
        Re: when to include &lt;string&g t;

        On Feb 8, 1:03 pm, Travis <travis.bow...@ gmail.comwrote:
        So you're saying that by including <iostream>, it then includes
        <string>?
        For your implementation, it appears that the iostream header includes
        the string header, but as others have stated, that is not guaranteed
        to be the case in other implementations . In other words, you could
        try to compile your code with another compiler and it might fail. May
        as well be safe and include <stringin your code.

        Comment

        • Travis

          #5
          Re: when to include &lt;string&g t;

          On Feb 8, 12:16 pm, dave_mikes...@f astmail.fm wrote:
          On Feb 8, 1:03 pm, Travis <travis.bow...@ gmail.comwrote:
          >
          So you're saying that by including <iostream>, it then includes
          <string>?
          >
          For your implementation, it appears that the iostream header includes
          the string header, but as others have stated, that is not guaranteed
          to be the case in other implementations . In other words, you could
          try to compile your code with another compiler and it might fail. May
          as well be safe and include <stringin your code.
          Thanks

          Comment

          Working...