From: Manuel Nickschas Date: Mon, 10 Sep 2018 20:12:44 +0000 (+0200) Subject: common: Provide helper for resolving overloaded function pointers X-Git-Tag: test-travis-01~130 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=42ab7cc22c4702716db2b8bfa1d4545169f772e6;hp=a0e333e994dce2d949a84930293382020e724596 common: Provide helper for resolving overloaded function pointers The pointer-to-member-function connect syntax cannot directly deal with overloaded method signatures. Provide a helper to avoid ugly static_cast incantations: #include "util.h" connect(this, selectOverload(&MyClass::mySignal), other, &Other::mySlot); This helper can be used for both signals and slots. --- diff --git a/src/common/util.h b/src/common/util.h index 63d724c9..cead16fa 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -105,3 +105,27 @@ COMMON_EXPORT QString tryFormatUnixEpoch(const QString &possibleEpochDate, * @return Date/time in ISO 8601 format with timezone offset */ COMMON_EXPORT QString formatDateTimeToOffsetISO(const QDateTime &dateTime); + +namespace detail { + +template +struct SelectOverloadHelper +{ + template + constexpr auto operator()(R(C::*func)(Args...)) const noexcept -> decltype(func) { return func; } +}; + +} // detail + +/** + * Helper for resolving ambiguous overloads when using the member function-based connect syntax. + * + * Example usage: + * @code + * connect(this, selectOverload(&MyClass::mySignal), other, &Other::mySlot); + * @endcode + * + * @tparam Args Argument types of the desired signature + */ +template +constexpr Q_DECL_UNUSED detail::SelectOverloadHelper selectOverload = {};