Search code examples
c++move

NotImplementedException C++


From the answer here I implemented my class NotImplementedException

//exceptions.h
namespace base
{
    class NotImplementedException : public std::logic_error
    {
    public:
        virtual char const* what() { return "Function not yet implemented."; }
    };
}

In another class I would like to throw the following exception (same namespace):

    std::string to_string() override
    {
        throw NotImplementedException();
    }

The to_string method is an overriden method from an abstract base class.

namespace BSE {
    class BaseObject
    {
        virtual std::string to_string() = 0;
    };
}

Unfortunately, the compilation of the previous code shows me this error:

error C2280: BSE::NotImplementedException::NotImplementedException(void)': attempting to reference a deleted function`

From here I understood that the problem it has something to do with move constructor or assignment which according to cppreference.com - throw (1) this could be the case:

First, copy-initializes the exception object from expression (this may call the move constructor for rvalue expression, and the copy/move may be subject to copy elision)

I tried adding

    NotImplementedException(const NotImplementedException&) = default;
    NotImplementedException& operator=(const NotImplementedException&) = default;

to my class but that gives me

error C2512: 'BSE::NotImplementedException': no appropriate default constructor available

and as far as I know std::logic_error has no default constructor defined.

Q:How do I get around this?


Solution

  • It should be something like:

    namespace base
    {
        class NotImplementedException : public std::logic_error
        {
        public:
            NotImplementedException () : std::logic_error{"Function not yet implemented."} {}
        };
    }
    

    And then

    std::string to_string() override
    {
        throw NotImplementedException();
    }