You can forward declare your own classes in header files to save compilation time. But you can’t for classes in namespace std. According to the C++11 standard, 17.6.4.2.1:
The behavior of a C++ program is undefined if it adds declarations or
definitions to namespace std or to a namespace within namespace std
unless otherwise specified.
Note that some of these classes are typedefs of templated classes, so a simple forward declaration will not work. You can use #include<iosfwd>
instead of #include<iostream>
for example, but there are no similar headers with just forward declarations for string
, vector
, etc.
See GotW #34, Forward Declarations for more information.