调用成员函数(如果存在),回退到自由函数,反之亦然

Calling a member function if it exists, falling back to a free function and vice-versa

本文关键字:函数 回退 自由 反之亦然 存在 成员 如果 调用      更新时间:2023-10-16

我可以编写一个模板函数,该函数采用参数 T 调用成员函数foo如果它存在于T上,如果它不调用自由函数foo(T)而是(如果两者都不存在,则无法编译)?

像这样:

template<typename T>
int call_foo(T t) {
// if T::foo() exists
return t.foo();
// else return foo(t);
}

相反的情况怎么样:首选自由函数foo成员函数之前?我无法使用 C++11 之后引入的任何功能。

这并不难。有许多方法可以检查任意表达式是否有效。您可以将其与 C++17 中的if constexpr相结合,或者更早地标记调度以获得您想要的行为。

这使用 C++17,但一切都可以在以前的版本中完成:

#include <type_traits>
#include <utility>
// This is just one way to write a type trait, it's not necessarily
// the best way. You could use the Detection Idiom, for example
// (http://en.cppreference.com/w/cpp/experimental/is_detected).
template <typename T, typename = void>
struct has_member_fn
: std::false_type
{};
// std::void_t is a C++17 library feature. It can be replaced
// with your own implementation of void_t, or often by making the
// decltype expression void, whether by casting or by comma operator
// (`decltype(expr, void())`)
template <typename T>
struct has_member_fn<T,
std::void_t<decltype(std::declval<T>().foo())>>
: std::true_type
{};

template <typename T, typename = void>
struct has_free_fn
: std::false_type
{};
template <typename T>
struct has_free_fn<T,
// Be wary of ADL. You're basically asking the compiler,
// "What's the result of foo(T{}) if I were to call that
// here?" That syntax can call functions via ADL
std::void_t<decltype(foo(std::declval<T>()))>>
: std::true_type
{};

template <typename T>
int call_foo(T t) {
// if constexpr is C++17, but you can use tag dispatch to
// do the same in prior versions
if constexpr (has_member_fn<T>::value) {
return t.foo();
} else {
// you could make this an `else if constexpr (has_free_fn<T>::value)`
// and provide a better case for if neither exists
return foo(t);
}
}

住在戈博尔特上

在 C++17 之前,您可以使用if constexpr编译/不编译同一函数的不同部分。

所以,在C++17之前,你必须在某个地方做两个不同的功能。

示例:如果您准备了几个帮助程序函数

template <typename T>
auto call_foo_h (T t, int) -> decltype( t.foo() )
{ return t.foo(); }
template <typename T>
auto call_foo_h (T t, long) -> decltype( foo(t) )
{ return foo(t); }

仅当T::foo()存在(第一个)或存在免费foo()(第二个)时才启用 SFINAE,您可以按如下方式编写call_foo()

template <typename T>
int call_foo (T const & t)
{ return call_foo_h(t, 0); }
//......................^ a int value

观察call_foo_h()中的第二个(未使用的)参数;T::foo()版本中的int,自由版本中的long

诀窍是:使用int(0)调用call_foo_h,您最好调用int版本(T::foo()),否则称为long版本。

相反的情况怎么样:首选自由函数foo在成员函数之前?

在这种情况下,按如下方式编写call_foo()

template <typename T>
int call_foo (T const & t)
{ return call_foo_h(t, 0L); }
//......................^^ a long value

即:使用long值调用call_foo_h,优先于免费foo()版本。