C++: String data type

N

Newsnet Customer

Hi,

Which system header file do I unclude in order to use the new string data
type?


KJ
 
J

Jon Bell

2) I assume system header files provides implementations unlike user-defined
header files, which are just interfaces.

No, the system header files are generally just interfaces also. An
exception may be the header files that declare template classes, because
many or most compilers don't support separate compilation of templates.

The implementations are usually in a compiled library file that your
compiler automatically links to your compiled code.
 
N

Newsnet Customer

No, the system header files are generally just interfaces also. An
exception may be the header files that declare template classes, because
many or most compilers don't support separate compilation of templates.

The implementations are usually in a compiled library file that your
compiler automatically links to your compiled code.

Right, so your saying that by including:

#include <string>

the compiler's preprocessor will include the contents of the <string> header
file into the source, then compile the entire source file, and then link it
with whatever file it neeeds (in this case, string.cpp). Is this what you
mean?


Cheers
djfh
 
N

Newsnet Customer

That's not a question. As an assumption, it's incorrect. For a start, in
the statement '#include <string>', the word 'string' is not a filename.
The necessary declarations, etc., may or may not be stored in a file.


if string is not a filename then what file is it actually including in this
case? an object string?
 
M

Mike Wahler

Newsnet Customer said:
if string is not a filename then what file is it actually including in this
case? an object string?

The name of the *header* is <string>. The statement:

#include <string>

Is simply required to cause the compiler to provide
all declarations the language specifies it must, at
the scope where the #include directive appears.

A compiler is free to provide these delcarations any
way at all, e.g. via a file, 'hard coded' into the
compiler, or any other way at all. It's very common
for standard headers to be represented with files,
but not at all required.

-Mike
 
R

Ron Natalie

Mike Wahler said:
A compiler is free to provide these delcarations any
way at all, e.g. via a file, 'hard coded' into the
compiler, or any other way at all. It's very common
for standard headers to be represented with files,
but not at all required.

Not exactly right. The section header for #include
says "SOURCE FILE INCLUSION." The input units of C++ programs
are defined as SOURCE FILES in the standard. Of course, the C++
notion of source file may be different than what any particular
operating system's idea of a file is. As far as C++ is concerned
a source file is just a stream of implentation defined characters
that subsequently get processed into the source character set and then
into tokens.

Using a system that just uses a database of declarations does not meet
the C++ model (as those who were unfortunate enough to use IBM's attempt
at doing C++ that way found out).
 
M

Michiel Salters

Newsnet Customer said:
if string is not a filename then what file is it actually including in this
case? an object string?

Or a precompiled version that does exactly what the file would have done.
Or any other representation that makes the implementation behave as if
a file containg the definitions was included.

HTH,
 
M

Michiel Salters

Buster Copley said:
0Newsnet Customer wrote: ,snip>
That said, in many cases there will be a header file called 'string'.
And because separate compilation of templates is so tricky, the chances
are most of the implementation will be available just from including the
appropriate header. User header files may also include templates and
inline functions.

<string> is special, because in 99,x% of the cases it is instantiated on
either char or wchar_t, so for these two cases an implementation may very
have a separation of interface and implementation. In addition, this can
be achieved in non-portable ways, because the <string> implementation
is a compiler-specific header.

Regards,
 
K

Kevin Goodsell

Newsnet said:
Right, so your saying that by including:

#include <string>

the compiler's preprocessor will include the contents of the <string> header
file into the source, then compile the entire source file, and then link it
with whatever file it neeeds (in this case, string.cpp). Is this what you
mean?

It is very unlikely that this is what happens. Looking at it abstractly,
the implementation may not include a file called 'string'. It may not
have such a file at all. It's possible that it uses some magic to
replace the #include directive with the right stuff. It's also possible
that it doesn't do anything except set some kind of flag in the
compiler. "Header file" is not the right term, since it may or may not
be an actual file. The term is simply "header".

In most real implementations, 'string' is the name of a real file, and
the contents of that file are inserted into the source file at the point
of the #include directive. However, it's extremely unlikely that there
would be a 'string.cpp' file that the compiler would link in, for
several reasons.

First, compiler libraries are usually .lib or .obj files, and they
usually don't have one for every header. So it would be more likely to
have something like cppstd.lib that contains all or most of the standard
library (except for templates).

Second, std::string is a specialization of a template
(std::basic_string), and templates cannot be separately compiled in most
implementations. So 'string.cpp' wouldn't work. It's more likely that
the entire implementation of the basic_string template is in the
<string> header.

But basically you are asking hypothetical questions that don't have any
single answer and don't affect language usage anyway. So why bother
worrying about them?

-Kevin
 
N

Newsnet Customer

The name of the *header* is <string>. The statement:

#include <string>

Is simply required to cause the compiler to provide
all declarations the language specifies it must, at
the scope where the #include directive appears.

A compiler is free to provide these delcarations any
way at all, e.g. via a file, 'hard coded' into the
compiler, or any other way at all. It's very common
for standard headers to be represented with files,
but not at all required.

I asummed that if the header does not have an extension associated with it
then it's not a file, which leaves me thinking what <string> represents. I
don't think you have exactly told me that, but it appears that it could be
'hard-coded' into the compiler.


kdf
 
S

Sam Holden

I asummed that if the header does not have an extension associated with it
then it's not a file, which leaves me thinking what <string> represents. I
don't think you have exactly told me that, but it appears that it could be
'hard-coded' into the compiler.

Your assumption is incorrect. The header could be implemented as a file,
or it could just set a flag in the compiler, or whatever.

As a counter example to your assumption:

$ ls -l /gnu/usr/include/g++-3/string
-rw-rw-r-- 1 bin bin 238 Nov 18 1999 /gnu/usr/include/g++-3/string

That compiler/library combination implementation uses a file named 'string' to
perform #include <string>.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top