Why Does Omission of "#Include ≪String≫" Only Sometimes Cause Compilation Failures

Why does omission of #include string only sometimes cause compilation failures?

If you use members that are declared inside the standard header string then yes, you have to include that header either directly or indirectly (via other headers).

Some compilers on some platforms may on some time of the month compile even though you failed to include the header. This behaviour is unfortunate, unreliable and does not mean that you shouldn’t include the header.

The reason is simply that you have included other standard headers which also happen to include string. But as I said, this can in general not be relied on and it may also change very suddenly (when a new version of the compiler is installed, for instance).

Always include all necessary headers. Unfortunately, there does not appear to be a reliable online documentation on which headers need to be included. Consult a book, or the official C++ standard.

For instance, the following code compiles with my compiler (gcc 4.6):

#include <iostream>

int main() {
std::string str;
}

But if I remove the first line, it no longer compiles even though the iostream header should actually be unrelated.

why I can use the string function length() when the string isn't included in C++?

Because internally iostream includes string.

Includes are transitive, and some of the dependencies might be compiler dependent, while others will be the same on all platforms (one such example is that including map will make pair available, as it depends on it directly).

The dependency between string and iostream isn't defined anywhere, so while it might work on some compilers, you shouldn't depend on it.

Is string header file needed anymore?

Some compiler implementations include the header <string> in the header <iostream>.

But you shall not rely on this.

Why does omission of #include string only sometimes cause compilation failures?

If you use members that are declared inside the standard header string then yes, you have to include that header either directly or indirectly (via other headers).

Some compilers on some platforms may on some time of the month compile even though you failed to include the header. This behaviour is unfortunate, unreliable and does not mean that you shouldn’t include the header.

The reason is simply that you have included other standard headers which also happen to include string. But as I said, this can in general not be relied on and it may also change very suddenly (when a new version of the compiler is installed, for instance).

Always include all necessary headers. Unfortunately, there does not appear to be a reliable online documentation on which headers need to be included. Consult a book, or the official C++ standard.

For instance, the following code compiles with my compiler (gcc 4.6):

#include <iostream>

int main() {
std::string str;
}

But if I remove the first line, it no longer compiles even though the iostream header should actually be unrelated.

Why does omission of #include string only sometimes cause compilation failures?

If you use members that are declared inside the standard header string then yes, you have to include that header either directly or indirectly (via other headers).

Some compilers on some platforms may on some time of the month compile even though you failed to include the header. This behaviour is unfortunate, unreliable and does not mean that you shouldn’t include the header.

The reason is simply that you have included other standard headers which also happen to include string. But as I said, this can in general not be relied on and it may also change very suddenly (when a new version of the compiler is installed, for instance).

Always include all necessary headers. Unfortunately, there does not appear to be a reliable online documentation on which headers need to be included. Consult a book, or the official C++ standard.

For instance, the following code compiles with my compiler (gcc 4.6):

#include <iostream>

int main() {
std::string str;
}

But if I remove the first line, it no longer compiles even though the iostream header should actually be unrelated.

Why does this C++ program compile and run despite required header files being commented out?

In C++ (unlike C) including any one standard header is allowed to do the equivalent of including any or all other standard headers. So, if you include <iostream> that can be equivalent to also including <algorithm>, <vector>, <string>, etc.

Early on, many compilers/libraries took advantage of this a great deal. Recently, they tend to be closer to a header only declaring/defining what it's required to, but there are still some headers that indirectly include at least some others.

error C2679: binary '': no operator found which takes a right-hand operand of type 'std::string_view' (or there is no acceptable conversion)

Like others already mentioned - std::string_view is defined in the standard header "string_view", which must be included - otherwise string_view is not defined.

Because you include some headers like "string" and "iostream" which have some connection to std::string_view it is clear that they do at least some forward_declarations.

In some implementations string_view might be already included in other system headers.
For example std::string_view could be implemented/defined in the header "string" and the header "string_view" could just include "string".

But in general this is an implementation detail of the library implementation. To be able to use std::string_view, it is required to include that header.



Related Topics



Leave a reply



Submit