gpt4 book ai didi

c++ - 无法使方法存在检测机制起作用

转载 作者:太空狗 更新时间:2023-10-29 20:50:14 24 4
gpt4 key购买 nike

我正在编写模板化代码,它需要调用它获取的仿函数的某个模板化 operator() - 但前提是该 operator() 存在。

我写了下面的代码:


template <typename>
struct sfinae_true : std::true_type{};

template <class F, typename T, typename... Us>
static auto test_templated_invoke_operator(int) ->
sfinae_true<decltype(std::declval<F>().template operator()<T>(std::forward(std::declval<Us>())... ))>;

template <class, typename, typename... Us>
static auto test_templated_invoke_operator(long) -> std::false_type;

template <class F, typename T, typename... Us>
struct has_templated_invoke_operator : decltype( test_templated_invoke_operator<F, T, Us...>(int{}) )
{ };

template <bool ActuallyInvoke, typename R, class F, typename T, typename... Ts>
struct invoke_if_possible_inner;

template <class F, typename R, typename T, typename... Ts>
struct invoke_if_possible_inner<false, R, F, T, Ts...>
{
R operator()(F, Ts&&...) {
return R();
}
};

template <class F, typename R, typename T, typename... Ts>
struct invoke_if_possible_inner<true, R, F, T, Ts...>
{
R operator()(F functor, Ts&&... params)
{
return functor.template operator()<T>(std::forward<Ts>(params)...);
}
};

template <typename T, typename R>
struct invoke_if_possible {
template <class F, typename... Ts>
R operator()(F functor, Ts&&... params)
{
constexpr bool actually_invoke = has_templated_invoke_operator<F, T, Ts...>::value;
// static_assert(actually_invoke == true,"Should be able to invoke for now!");
return invoke_if_possible_inner<actually_invoke, R, F, T, Ts...>{}(functor, std::forward<Ts>(params)...);
}
};

这里有一个小的 main() 函数来测试它:

int main() 
{
invoke_if_possible<int, double> iip;
auto result = iip(foo{}, 3.0);
std::cout << "Invoke if possible result is " << result << " (and should be 6.0)" << std::endl;
}

fails (Coliru) - 返回 0.0 而不是 6.0。

我的问题是:为什么代码不调用定义的 operator()?以及如何修复检测机制,以便承认并调用它的存在?

注意事项:

  • 模板参数T是任意的;它与 Us 参数无关。
  • 是的,可以使用 operator() 的返回类型派生 R - 但前提是它存在。所以我们只是提供它。
  • 如果启用静态断言 - 它会失败。

最佳答案

你的问题在这里:

std::forward(std::declval<Us>())

std::forward采用您未提供的非推导模板参数 - 必须是 forward<T>(u) - 所以它的推论无条件地失败了。

但你甚至不需要 forward在这里。就declval<Us>()足够了。

关于c++ - 无法使方法存在检测机制起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55776371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com