Skip to content

Commit

Permalink
COMP: Define ExceptionObject destructor out-of-line (doing Rule of Five)
Browse files Browse the repository at this point in the history
Followed the C++ Rule of Five, instead of the Rule of Zero, for
`ExceptionObject`, and added the definition of its destructor to its CXX
file.

Aims to fix Visual C++ link errors (error LNK2005 "symbol was defined
more than once", fatal error LNK1169) reported by Simon Rit, and
confirmed by Lucas Gandel, at "Modernize the implementation of
ExceptionObject", pull request InsightSoftwareConsortium#2374
  • Loading branch information
N-Dekker authored and dzenanz committed Mar 18, 2021
1 parent faf3d3b commit 0fce3ff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Modules/Core/Common/include/itkExceptionObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ class ITKCommon_EXPORT ExceptionObject : public std::exception
static constexpr const char * const default_exception_message = "Generic ExceptionObject";
using Superclass = std::exception;

/** Explicitly-defaulted default-constructor. Creates an empty exception object.
* \note The other five "special member functions" (copy-constructor,
* copy-assignment operator, move-constructor, move-assignment operator,
* and destructor) are defaulted implicitly, following the C++ "Rule of Zero".
* All of these special member functions are `noexcept`.
*/
/** Explicitly-defaulted default-constructor. Creates an empty exception object. */
ExceptionObject() noexcept = default;

explicit ExceptionObject(const char * file,
Expand All @@ -67,6 +62,24 @@ class ITKCommon_EXPORT ExceptionObject : public std::exception
std::string desc = "None",
std::string loc = "Unknown");

/** Copy-constructor. */
ExceptionObject(const ExceptionObject &) noexcept = default;

/** Move-constructor. */
ExceptionObject(ExceptionObject &&) noexcept = default;

/** Copy-assignment operator. */
ExceptionObject &
operator=(const ExceptionObject &) noexcept = default;

/** Move-assignment operator. */
ExceptionObject &
operator=(ExceptionObject &&) noexcept = default;

/** Destructor.
* \note It appears necessary to define the destructor "out-of-line" for external linkage. */
~ExceptionObject() override;

/** Equivalence operator. */
virtual bool
operator==(const ExceptionObject & orig) const;
Expand Down
4 changes: 4 additions & 0 deletions Modules/Core/Common/src/itkExceptionObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ ExceptionObject::ExceptionObject(std::string file, unsigned int lineNumber, std:
{}


// Note: It appears necessary to define the destructor "out-of-line" for external linkage.
ExceptionObject::~ExceptionObject() = default;


bool
ExceptionObject::operator==(const ExceptionObject & orig) const
{
Expand Down

0 comments on commit 0fce3ff

Please sign in to comment.