🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2326

Re: C++ Standard

#2326
From: Lars Farm
Date: Fri, 27 Oct 1995 14:55
85 lines
4197 bytes
In article <46mads$qvv@turtle.hearst.com>,
PWLee@@ns1.hearst.com wrote:

>In article <ACB2F42C9668BCA1@sleipner.nts.mh.se>
>           lars.farm@nts.mh.se "Lars Farm" writes:
> > The diagnostic exception classes are constructed from a const string&.
> > Will they copy the text or store a reference to the string?

> I have a slightly different thought on this .. why do the constructors
>require a string& instead of a const char * ?

I'd like to see a const char* ctor in addition to the string& ctor. Perhaps
this is already allowed?

> (The whole thing is further
>obfuscated by the base exception class not having a constructor which takes
>the const string& parameter).
>
>IMHO, it would be highly desireable if the base class had such a
>constructor (perhaps protected) which takes such a parameter. I am keen to
>see this in the standard, since it is the obvious implementation...which
>I expect most vendors to follow, but if it is not standardized, then there
>will always be someone who doesn't.

class exception can not be allowed to carry a string object because class
bad_alloc is derived from it, as are the other language support exception
classes. If you're out of RAM and wants to throw bad_alloc, then you might
not be able to construct the string in the base class and thus not be able
to throw bad_alloc. Bad. None of the language support exception classes
needs any data at all. I suppose exception::what() could be implemented:

  const char*exception::what() const { return typeid(*this).name(); }

which would be sufficient for all the language support exceptions.

>(I am thinking here primarily of user-defined exception classes, which
>derive from exception, but not from logic_error or runtime_error)
>
>Thoughts, anyone?

For your own exception classes you might perhaps do the same as
runtime_error/logic_error. Introduce your own base with whatever data/ctors
you need:

    class my_exception_base : public exception {
      string s;
    public:
      my_exception_base(const string& what_arg) :s(what_arg){}
      const char* what() const { return s.c_str(); }
    };
    class my_kindof_error1 : public my_exception_base { ... };
    class my_kindof_error2 : public my_exception_base { ... };
    ...

This brings me back to my original question, so far unanswered. I asked
because I disagree with my compiler vendor about interpretation of the
draft. I believe that the diagnostic exceptions (file <stdexcept>) should
carry a copy of the text. They do not, and implements it with a reference
to string, like so:

    class logic_error : public exception {
      const string& s;
    public:
      logic_error(const string& what_arg) :s(what_arg){}
      const char* what() const { return s.data(); }
    };

The draft says (string& ctor): "post:what()==what_arg.data()" which taken
literaly supports their interpretation. Can this really be the intent? (see
above referenced article) If so, you can't say 'throw
logic_error("error")'. You must init the exception object from a static
string object. This in turn means you can't throw standard exceptions from
a catch handler... So I think that "what()==what_arg.data()" must be
interpreted "two identical texts" rather than "one shared text"

Does anyone know for sure?

 - Lars

--
Lars Farm, lars.farm@nts.mh.se

[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <ACB6A1F6966852419@sleipner.nts.mh.se>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
References: <46mads$qvv@turtle.hearst.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: lars.farm@nts.mh.se (Lars Farm)
Newsgroups: comp.std.c++
Subject: Re: C++ Standard
Date: 27 Oct 1995 14:55:25 GMT
Organization: pv
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <ACB6A1F6966852419@sleipner.nts.mh.se>
References: <46mads$qvv@turtle.hearst.com>
NNTP-Posting-Host: taumet.eng.sun.com
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"; conversions="8bit"
Content-Transfer-Encoding: 8bit
X-Nntp-Posting-Host: @sleipner.nts.mh.se
X-Lines: 81
Content-Length: 3301
Originator: clamage@taumet