This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Problem inheriting from std::exception


Hi,

I took a look at this bug report in GNATS:
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=1900&database=gcc

I modified the example so that it would compile with gcc 2.97:

=======================================================================
 
 #define DEBUG
 
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <string>
#include <iostream>
 
namespace gutils {
 
  class Exception : public std::exception
  {
  public:
     Exception
     (
      const std::string& what_arg,
      const char* file = 0,
      unsigned long line = 0
      ) : e_what_(what_arg), file_(0), line_(line)
    {
      if (file)
        file_ = new std::string(file);
    }
 
    virtual ~Exception() throw () { delete file_; }
 
    virtual const char* what() const throw() { return e_what_.c_str(); }
 
    virtual const char* file() const
    {
      if (file_)                                         
        return file_->c_str();
 
      return 0;
    }
 
    virtual unsigned long line() const { return line_; }
 
  private:
    std::string e_what_;
    std::string* file_;
    unsigned long line_;
  }; // class Exception
 
 #ifdef DEBUG
 #define EXCEPTION(WHAT) gutils::Exception((WHAT), __FILE__, __LINE__)
 #else
 #define EXCEPTION(WHAT) gutils::Exception((WHAT))
 #endif
  class LogicError : public std::logic_error, public gutils::Exception
  {
  public:
     LogicError
     (
      const std::string& what_arg,
      const char* file = 0,
      unsigned long line = 0
      ) : logic_error(what_arg), Exception(what_arg, file, line) {}
 
    virtual const char* what() const throw() { return Exception::what(); }
 
    // virtual ~LogicError() { }
  };
 
 #ifdef DEBUG
 #define LOGIC_ERROR(WHAT) gutils::LogicError((WHAT), __FILE__, __LINE__)
 
 #else
 #define LOGIC_ERROR(WHAT) gutils::LogicError((WHAT))
 #endif
 
}; // namespace gutils                

==============================================================================

If I compile this code with gcc 2.97, it coredumps when I try to run it.
When I compiled it with the Digital Unix C++ compiler, it compiled
and ran fine.
Is this a bug in the code, or a bug in libstdc++-v3?
I am not familiar enough with the Standard
to track this bug down.

Any help would be appreciated.
-- 
Craig Rodrigues        
http://www.gis.net/~craigr    
rodrigc@mediaone.net          


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]