Topic: Declaring std::string ?
Author: Darin Adler <darin@bentspoon.com>
Date: 2000/03/16 Raw View
In article <8ao2oe$iif$1@nnrp1.deja.com>, Philip Brabbin
<pabrabbin@hotmail.com> wrote:
> If I have a class member function which has the prototype:
>
> bool Compare ( const std::string &s ) ;
>
> I have to provide a declaration for the type 'string'. What is the best
> way to do this ? Clearly the simplest way is to use
>
> #include <string>
>
> but this seems to be complete overkill since <string> contains far more
> than I require, crippling my compilation times. If I knew that
> std::string were a class, I could use a forward declaration:
>
> namespace std {
> class string;
> }
>
> which would do the trick. However, my understanding is that std::string
> is in fact a typedef for std::basic_string<char>:
>
> namespace std {
> template <class T> class basic_string;
> typedef basic_string<char>;
> }
>
> So is this the answer ? Do I have to write all this ever time I want
> to refer to std::string in a header file, or is there a better way ????
Unfortunately, according to the standard the only way that is guaranteed
to work is "#include <string>".
In past discussions of this, some have suggested a <stringfwd> header to
address this issue, which declares enough of std::string for this kind
of use. Others have claimed that the effect on compile times is really
not as great as it seems.
-- Darin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/03/16 Raw View
Philip Brabbin wrote:
>
> If I have a class member function which has the prototype:
>
> bool Compare ( const std::string &s ) ;
>
> I have to provide a declaration for the type 'string'. What is the best
> way to do this ? Clearly the simplest way is to use
>
> #include <string>
It's also the only way, since there's no <stringfwd> similar
to <iosfwd>.
>
> but this seems to be complete overkill since <string> contains far more
> than I require, crippling my compilation times. If I knew that
> std::string were a class, I could use a forward declaration:
>
> namespace std {
> class string;
> }
Even if std::string were a class, you wouldn't be allowed to
do this, since you may not introduce names into namespace std;
only the standard headers may do this (real compilers most likely
let you do it anyway, though - the non-class objection for
string remains, of course).
>
> which would do the trick. However, my understanding is that std::string
> is in fact a typedef for std::basic_string<char>:
>
> namespace std {
> template <class T> class basic_string;
> typedef basic_string<char>;
> }
Even if adding to namespace std were allowed, this would be
wrong, since std::string is actually defined as
namespace std
{
template<class _CharType,
class _Traits = char_traits<_CharType> > class basic_string;
typedef basic_string<char> string; // using default parameter
}
However, even that could fail, since an implementation might add
further default parameters, which conforming code cannot tell
(as it doesn't use them), f.ex.:
namespace std
{
template<class _CharType,
class _Traits = char_traits<_CharType>,
__storage_type _Storage = __coniguous_storage>
class basic_string;
typedef basic_string<char> string;
}
>
> So is this the answer ? Do I have to write all this ever time I want
> to refer to std::string in a header file, or is there a better way ????
There's only one way:
#include <string>
Nothing else is guaranteed to work.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Max Polk <none@none.invalid>
Date: 2000/03/17 Raw View
> In article <8ao2oe$iif$1@nnrp1.deja.com>, Philip Brabbin
> <pabrabbin@hotmail.com> wrote:
[EDITED]
> > #include <string>
> > seems to be complete overkill since <string> contains far more
> > than I require, crippling my compilation times.
In article <darin-954DBD.11105615032000@fullnews.metawire.com>,
darin@bentspoon.com says...
> In past discussions of this, some have suggested a <stringfwd> header to
> address this issue, which declares enough of std::string for this kind
> of use. Others have claimed that the effect on compile times is really
> not as great as it seems.
I think we all agree a simple "#include <string>" should not cripple
anything. It is a completely natural, very frequently occurring line of
code. The only solution I have seen is to use pre-compiled headers to
work through this mess, but if your compiler writer didn't add this
support, you are out of luck.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Philip Brabbin <pabrabbin@hotmail.com>
Date: 2000/03/16 Raw View
If I have a class member function which has the prototype:
bool Compare ( const std::string &s ) ;
I have to provide a declaration for the type 'string'. What is the best
way to do this ? Clearly the simplest way is to use
#include <string>
but this seems to be complete overkill since <string> contains far more
than I require, crippling my compilation times. If I knew that
std::string were a class, I could use a forward declaration:
namespace std {
class string;
}
which would do the trick. However, my understanding is that std::string
is in fact a typedef for std::basic_string<char>:
namespace std {
template <class T> class basic_string;
typedef basic_string<char>;
}
So is this the answer ? Do I have to write all this ever time I want
to refer to std::string in a header file, or is there a better way ????
Any suggestions much appreciated,
- Phil Brabbin
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]