On Saturday, 5 November 2016 10:46:28 UTC+2, Christopher J. Pisz wrote:
> I am reading about the "copy and swap idiom" online, which is new to me.
Whatever it is they write about they often praise it but forget
to mention the important limitations.
> I desire to derive from std::runtime error in order to make my own
> exception that also contains filename, line, and time it was created.
Note that "copy and swap" won't typically make sense on case of
dynamically polymorphic object hierarchy like 'std::exception'.
You will get objects sliced if you swap within such hierarchy using
base classes.
>
> I do not see a move constructor, or a swap for any of the std
> exceptions. Are there any?
No. Also compiler may not make those implicitly.
>
> Do I just not worry about "the rule of 5" here and stick to the "rule of
> 3" that I do know?
>
> I am not sure how I would swap that what() from within a derived class.
I am not sure why the copy constructor of 'std::exception' is not
'protected' and copy assignment '= delete'. Perhaps there was
something about exceptions that I have forgotten. Why you copy
exceptions? I just throw and catch const references to those.
The usual rule about dynamically polymorphic stuff is that we use
it through additional indirection.
When we need to swap or to assign such then we swap or assign owning
pointers or smart pointers and we do not assign nor copy nor swap
the object itself.
When we really need to dynamically create polymorphic copy of actual
objects (rare case) then we add virtual 'clone' function to the
whole hierarchy.