Topic: <string> depends on <stdexcept> which depends on <string>?
Author: sorry@but.spammed.out (Howard Hinnant)
Date: 1997/12/29 Raw View
It appears to me that <string> and <stdexcept> have a cyclical dependency
relationship. The classes in <stdexcept> must process strings, and
<string> must throw classes defined in <stdexcept>. Would someone let me
know why this is not a cyclic dependency?
Thanks,
Howard
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/12/30 Raw View
Howard Hinnant <sorry@but.spammed.out> wrote:
: It appears to me that <string> and <stdexcept> have a cyclical dependency
: relationship. The classes in <stdexcept> must process strings, and
: <string> must throw classes defined in <stdexcept>. Would someone let me
: know why this is not a cyclic dependency?
It is a cyclical dependancy. It's not good, but sometimes it's just
a fact of life and there is nothing you can do about it. Do you have
any particular problems with it?
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1997/12/30 Raw View
Hi,
Howard Hinnant (sorry@but.spammed.out) wrote:
: It appears to me that <string> and <stdexcept> have a cyclical dependency
: relationship. The classes in <stdexcept> must process strings, and
: <string> must throw classes defined in <stdexcept>. Would someone let me
: know why this is not a cyclic dependency?
There is no need at all for the definition of 'string' in '<stdexcept>'
and vice versa. For the declaration of the exception classes, it is
sufficient to know that 'string' is a typedef for 'basic_string<char>'
(which in turn also has to be declared before it can be used, ie. the
template class 'basic_string' has to be declared before the typdef for
'string' can be introduced). With the definitions (not only the
declarations) of the classes at hand, there is then no problem to
implement either of these classes.
--
<mailto:dietmar.kuehl@claas-solutions.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Pete Becker <petebecker@acm.org>
Date: 1997/12/30 Raw View
Howard Hinnant wrote:
>
> It appears to me that <string> and <stdexcept> have a cyclical dependency
> relationship. The classes in <stdexcept> must process strings, and
> <string> must throw classes defined in <stdexcept>. Would someone let me
> know why this is not a cyclic dependency?
Whoops, forgot to mention what the fix was: the standard exception
classes deal in const char *'s rather than strings.
-- Pete
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1997/12/31 Raw View
sorry@but.spammed.out (Howard Hinnant) writes:
> It appears to me that <string> and <stdexcept> have a cyclical dependency
> relationship. The classes in <stdexcept> must process strings, and
> <string> must throw classes defined in <stdexcept>. Would someone let me
> know why this is not a cyclic dependency?
Those two headers are more tightly coupled than I'm really comfortable
with, but it's not quite a circular dependency. It is implementable,
and several people (including me) have implemented it.
The point is that the interface in <stdexcept> does not rely on
anything about strings other than the fact that a string class exists.
That is, in <stdexcept> itself it can be an incomplete class
introduced by a forward declaration. You'd have another file
somewhere---a source file, not a header---that actually implemented
the classes in <stdexcept>. That file would include both <stdexcept>
and <string>.
Actually, the only tricky thing is the forward declaration itself;
it's slightly nontrivial, for two reasons. First, string isn't a
class but a typedef. Second, there's a language rule that says you
aren't allowed to declare a class template's default parameters more
than once in the same translation unit, even if the two declarations
agree.
So what you probably want to do is create a <stringfwd> header (or
something of a similar name), which would be included in both
<stdexcept> and <string>. It might look something like this.
#ifndef __string_forward
#define __string_forward
namespace std {
template <class T> class allocator;
template <class charT> class char_traits;
template <class charT,
class traits = char_traits<charT>,
class Alloc = allocator<charT> >
class basic_string;
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
}
#endif /* __string_forward */
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Pete Becker <petebecker@acm.org>
Date: 1997/12/31 Raw View
Howard Hinnant wrote:
>
> It appears to me that <string> and <stdexcept> have a cyclical dependency
> relationship. The classes in <stdexcept> must process strings, and
> <string> must throw classes defined in <stdexcept>. Would someone let me
> know why this is not a cyclic dependency?
Because the classes in stdexcept do not process strings. They did a
couple of years ago, but that was fixed. (Phew!)
-- Pete
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1997/12/31 Raw View
Pete Becker <petebecker@acm.org> writes:
> Howard Hinnant wrote:
> >
> > It appears to me that <string> and <stdexcept> have a cyclical dependency
> > relationship. The classes in <stdexcept> must process strings, and
> > <string> must throw classes defined in <stdexcept>. Would someone let me
> > know why this is not a cyclic dependency?
>
> Whoops, forgot to mention what the fix was: the standard exception
> classes deal in const char *'s rather than strings.
A slight clarification: the standard exception classes in <exception>
(exception, bad_exception), <new> (bad_alloc), and <typeinfo>
(bad_cast, bad_typeid) deal in const char* rather than in string.
However, the standard exception classes in <stdexcept> (logic_error,
runtime_error, domain_error, invalid_argument, length_error,
out_of_range, range_error, overflow_error, underflow_error) deal in
both const char* and in string. Each of those classes has a
constructor that takes a single argument of type string; the only
access to that string, however, is through the virtual member function
what(), which returns a const char*.
So, in fact, a library implementor does have to make sure that class
string is declared whenever <stdexcept> is included. The actual
definition, of course, can be deferred to whatever source file
implements the out-of-line constructors.
A forward declaration of string is a bit of a nuisance (it requires
declarations of three different class templates), but it's completely
straightforward. This aspect of the standard is implementable.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]