Skip to content

Commit

Permalink
Make error type bool conversion operators explicit
Browse files Browse the repository at this point in the history
This avoids ambiguities caused by overload resolution trying to use
these conversion operators to jump to other types in implicit conversion
(i.e. QTextStream& operator<<(QTextStream& ts, const Qx::Error e), but
still allows simple statements like:

if(errorDerivedInstance)

because these explicit conversion function are still allowed to be used
somewhat implicitly in "conditional contexts".

See https://en.cppreference.com/w/cpp/language/
implicit_conversion#Contextual%20conversions
  • Loading branch information
oblivioncth committed Oct 13, 2024
1 parent 62e5055 commit 3ab133c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/core/include/qx/core/qx-abstracterror.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ friend class Error;
public:
bool operator==(const AbstractError& other) const = default;
bool operator!=(const AbstractError& other) const = default;
operator bool() const { return deriveValue() > 0; };
explicit operator bool() const { return deriveValue() > 0; };
};

/* TODO: Get string of the type automatically when it becomes
Expand Down
2 changes: 1 addition & 1 deletion lib/core/include/qx/core/qx-error.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class QX_CORE_EXPORT Error
public:
bool operator==(const Error& other) const = default;
bool operator!=(const Error& other) const = default;
operator bool() const;
explicit operator bool() const;

//-Friend Functions------------------------------------------------------------------------------------------------
friend QTextStream& ::operator<<(QTextStream& ts, const Error& e);
Expand Down

0 comments on commit 3ab133c

Please sign in to comment.