2

I get a ICL compiler warning when inheriting from std::streambuf saying that the destructor is not compatible, any ideas what I'm doing wrong here? Making it a virtual destructor does not work either.

warning #809: exception specification for virtual function "CAbcBuffer::~CAbcBuffer" is incompatible with that of overridden function "std::basic_streambuf<_Elem, _Traits>::~basic_streambuf [with _Elem=char, _Traits=std::char_traits]"

class CAbcBuffer : public std::streambuf
{
    protected:
        /** Work buffer */
        char *buffer;
    public:
        explicit CAbcBuffer()
        {
            /*
            Stores the beginning pointer, the next pointer, and the end pointer for the 
            input buffer
            */
            buffer = new char[100];
            std::streambuf::setg(buffer, buffer, buffer);
        }

        ~CAbcBuffer() {
            delete [] buffer;
        }
}

1 Answers1

2

You are missing the throw() declaration for your destructor. This will fix the problem:

~CAbcBuffer() throw() {
    delete [] buffer;
}
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
  • That works. I will bump you up +1 if you explain why too. I thought throwing exceptions from the destructor was a bad idea so why does `std::streambuf` do so? Also the documentation says the destructor is a no-throw, see http://www.cplusplus.com/reference/streambuf/streambuf/~streambuf/ –  Mar 01 '14 at 19:33
  • 1
    Throwing exceptions from destructors *is* a horrible idea. But throw() means exactly that - it is a promise *not* to throw anything. – Christian Hackl Mar 01 '14 at 19:58
  • 2
    On the other hand, it's illegal for an implementation to declare the streambuf destructor no-throw. His original code is correct, and should work on a conforming implementation. – James Kanze Mar 01 '14 at 19:59
  • 1
    @James: you are right. It seems you cannot turn off this behaviour in MSVC unless you omit the /Za compiler switch, which I find very counter-intuitive, as /Za should *disable* MS-specific non-standard behaviour. – Christian Hackl Mar 01 '14 at 20:08