'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 1:[1]

Exception which doesn't say what is not implemented is quite annoying, so I would do this like this:

namespace base
{
    class NotImplementedException : public std::logic_error
    {
    public:
        using logic_error::logic_error;
         
        NotImplementedException(
                const std::source_location location = std::source_location::current())
            : logic_error{std::format("{} is not implemented!", location.function_name())}  
       {}
    };
}

Then

std::string to_string() override
{
    throw NotImplementedException("to_string for object");
    // or using source_location:
    throw NotImplementedException();
}

C++20 features are cool for this case, but not obligatory (and still not available on most compilers).

Some demo.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1