when to include <string>

T

Travis

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.
 
V

Victor Bazarov

Travis said:
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?

The rule of thumb is that if you need to _use_ a class defind in the
header <header>, then you need to include <header>. Simple as that.

So, if you define any object of type 'std::string', you need to do
the #include, according to my rule. If you need to declare an object
of type pointer to, or reference to, 'std::string', you don't really
need to include <string>, but you better since 'std::string' is
a typedef and you shouldn't really forward-declare what is a typedef
(although you could do it with a simple class, for example).

V
 
R

red floyd

Try either

using namespace std;

or

using std::string;

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

#include <string>
using namespace std;

or

You need to #include <string> whenever you use std::string. Either
explicitly as std::string, or via a using clause.
 
A

Andrey Tarasevich

Travis said:
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 <string> directly
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 <string> internally. For example, in some
implementation of standard library header <iostream> might include
header <string> internally, so when you include <iostream> you
automatically include <string> without 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> (directly or indirectly) into every translation unit
that uses 'std::string'.
 
J

James Kanze

The rule of thumb is that if you need to _use_ a class defind in the
header <header>, then you need to include <header>. Simple as that.

The rule of thumb is that if you refer to a class or a typedef
(std::string is the latter), then there must be a declaration
visible at that point. The only legal way to get a declaration
for std::string is to include <string>. Any reference to
std::string, in any way, shape, form or fashion, without having
So, if you define any object of type 'std::string', you need
to do the #include, according to my rule. If you need to
declare an object of type pointer to, or reference to,
'std::string', you don't really need to include <string>, but
you better since 'std::string' is a typedef and you shouldn't
really forward-declare what is a typedef (although you could
do it with a simple class, for example).

And even if std::string were a simple class, how would you
forward declare it, given that you're not allowed to declare
symbols in std. (I don't know if an implementation is allowed
to add additional template arguments---with default values---or
not. The standard doesn't seem to say. But of course, if an
implementation has this liberty, then you can't possibly forward
declare it, because you don't know what the actual forward
declaration should look like.)
 
T

Travis

If you want to use 'std::string' said:
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 <string> internally.

That's not entirely true. What prompted the question was acknowledging
the following compiles and works as intended.

// compile line: g++ main.cpp -o prog
#include <iostream>
int main()
{
std::string hello = "hello world";
std::cout << hello << std::endl;
std::cout << "size: " << hello.size() << std::endl;

return 0;
}

// output
$ ./prog
hello world
size: 11

Notice I didn't include any <string> class. So why does this work?

My original question stands...when do I need to include the string
class. Thanks.
 
D

dave_mikesell

That's not entirely true.

Yes, it is.
What prompted the question was acknowledging
the following compiles and works as intended.

// compile line: g++ main.cpp -o prog
#include <iostream>

Because, as was posted earlier, you are including something that
includes said:
My original question stands...when do I need to include the string
class. Thanks.

It can stand as long as it likes, but it was answered.
 
T

Travis

Yes, it is.



Because, as was posted earlier, you are including something that


It can stand as long as it likes, but it was answered.

So you're saying that by including <iostream>, it then includes
<string>?
 
A

Andrey Tarasevich

Travis said:
That's not entirely true. What prompted the question was acknowledging
the following compiles and works as intended.
...

It compiles and works _precisely_ for the reasons explained in my
previous message.
Notice I didn't include any <string> class. So why does this work?

"<string> class"? What's "<string> class"?

Just like I said before, what you have to do in order to use
'std::string' class is to include <string> header. Period.

And, as I said before, it is possible that this header might be included
indirectly (i.e. without your knowledge) through some other header. In
My original question stands...when do I need to include the string
class.

Your original question has been already answered repeatedly and
exhaustively. There's nothing else left to add. If you don't understand
something in the explanations given to you so far, please, point out
what exactly you don't understand. Blind repetition of the same question
won't help.
 
A

Alf P. Steinbach

* Travis:
That's not entirely true. What prompted the question was acknowledging
the following compiles and works as intended.

// compile line: g++ main.cpp -o prog
#include <iostream>
int main()
{
std::string hello = "hello world";
std::cout << hello << std::endl;
std::cout << "size: " << hello.size() << std::endl;

return 0;
}

// output
$ ./prog
hello world
size: 11

Notice I didn't include any <string> class. So why does this work?

My original question stands...when do I need to include the string
class. Thanks.

How about reading what you quoted.


Cheers, & hth.,

- Alf
 
P

peter koch

So you're saying that by including <iostream>, it then includes
<string>?

Perhaps we have misunderstood your question (or you asked the wrong
one ;-)).
If you use std::string you need to include <string>. There is no
standard header, that is guaranteed to be included indirectly. On your
implementation it just might happen that including <iostream> also
includes <string>, but it is a really stupid thing to rely on.
Otherwise you risk running into problems if you change compiler, or
simply upgrade it (or even change the compiler options). Include
<string> and be happy!

/Peter
 
J

James Kanze

That's not entirely true.

Yes it is.
What prompted the question was acknowledging
the following compiles and works as intended.
// compile line: g++ main.cpp -o prog
#include <iostream>
int main()
{
std::string hello = "hello world";
std::cout << hello << std::endl;
std::cout << "size: " << hello.size() << std::endl;
return 0;
}
// output
$ ./prog
hello world
size: 11
Notice I didn't include any <string> class. So why does this work?

Using std::string without including <string> is undefined
behavior. Anything can happen. Including that the program
seems to work.
My original question stands...when do I need to include the string
class.

And my original answer stands: any time you even mention
std::string, in any way, shape, form or fashion.
 
D

dave_mikesell

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 <string> in your code.
 
T

Travis

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 <string> in your code.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top