gpt4 book ai didi

c++ - 提取 "invokables"的签名

转载 作者:行者123 更新时间:2023-11-30 05:29:14 25 4
gpt4 key购买 nike

我在提取函数、成员函数和 lambda 的签名时遇到问题。我已经编写了下面的代码来完成它,但如果可能的话,我想要一个元函数来做同样的事情。现有解决方案的问题是,lambda 不能作为模板参数。

template <typename R, typename ...A>
struct signature
{
};

template <typename R, typename ...A>
constexpr auto num_args(signature<R, A...> const)
{
return sizeof...(A);
}

template <typename R, typename ...A>
constexpr auto extract_signature() noexcept
{
return signature<R, A...>();
};

template <typename C, typename R, typename ...A>
constexpr auto extract_signature(R (C::* const)(A...)) noexcept
{
return extract_signature<R, A...>();
}

template <typename C, typename R, typename ...A>
constexpr auto extract_signature(R (C::* const)(A...) const) noexcept
{
return extract_signature<R, A...>();
}

template <typename R, typename ...A>
constexpr auto extract_signature(R (*const)(A...)) noexcept
{
return extract_signature<R, A...>();
}

template <typename F>
constexpr auto extract_signature(F const& f) noexcept ->
decltype(&F::operator(), extract_signature(&F::operator()))
{
return extract_signature(&F::operator());
}

template <typename F>
constexpr auto extract_signature(F const& f) noexcept ->
decltype(f(), extract_signature(f))
{
return extract_signature(f);
}

我正在寻找类似的东西:

template <???>
struct signature_info
{
using type = signature<R, A...>;
};

元函数应该以某种方式提取RA...,如果可能的话,也许是通过decltype(lambda)

编辑:

::std::cout << ::std::integral_constant<::std::size_t, num_args(extract_signature([](int, int, int){}))>{} << ::std::endl;

会产生错误:

t.cpp:54:85: error: a lambda expression may not appear inside of a constant expression
::std::cout << ::std::integral_constant<::std::size_t, num_args(extract_signature([](int, int, int){}))>{} << ::std::endl;
^
t.cpp:54:58: error: non-type template argument is not a constant expression
::std::cout << ::std::integral_constant<::std::size_t, num_args(extract_signature([](int, int, int){}))>{} << ::std::endl;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.cpp:54:85: note: non-literal type '(lambda at t.cpp:54:85)' cannot be used in a constant expression
::std::cout << ::std::integral_constant<::std::size_t, num_args(extract_signature([](int, int, int){}))>{} << ::std::endl;

最佳答案

给定最后列出的代码片段。

这可以用与 std::function<> 类似的方式完成工作...

template <typename S>
struct signature_info

template <typename R, typename A...>
struct signature_info<R(A...)>
{
using type = signature<R, A...>;
static const std::size_t count = sizeof...(A);
};

可以扩展上述特化以支持您希望支持的所有函数类型。

例如,对于一个简单的 const成员函数;

template <typename C, typename R, typename... A>
struct signature_info<R(C::*)(A...) const>
{
using type = signature<R, A...>;
static const std::size_t count = sizeof...(A);
};

获取参数的计数 A... , 你可以使用 sizeof...(A..) .

有了元函数...

template <typename R, typename ...A>
struct signature
{
};

template <typename S>
struct signature_info;

template <typename R, typename... A>
struct signature_info<R(A...)>
{
using type = signature<R, A...>;
};

template <typename S>
signature_info<S> get_signature()
{
return signature_info<S>{};
}

void func(int);

int main()
{
auto abc = get_signature<decltype(func)>();
cout << abc.count << endl;
return 0;
}

组合 OP 代码(删除递归)和上述代码的完整工作示例 can be found here .


鉴于代码片段和编译器错误 - lambdas 不能用于 constexpr 的地方(核心常量表达式)是必需的(尽管在这方面提出了更改建议)。

为了编译成功,可以将代码修改为如下;

auto lambda = [](int, int, int){};
std::cout << ::std::integral_constant<::std::size_t, decltype(extract_signature(lambda))::count>{} << ::std::endl;

完整working sample here ;

#include <iostream>
#include <type_traits>
#include <utility>

using namespace std;
template <typename R, typename ...A>
struct signature
{
static const std::size_t count = sizeof...(A);
};

template <typename R, typename ...A>
constexpr auto extract_signature1() noexcept
{
return signature<R, A...>();
}

template <typename C, typename R, typename ...A>
constexpr auto extract_signature2(R (C::* const)(A...)) noexcept
{
return extract_signature1<R, A...>();
}

template <typename C, typename R, typename ...A>
constexpr auto extract_signature2(R (C::* const)(A...) const) noexcept
{
return extract_signature1<R, A...>();
}

template <typename R, typename ...A>
constexpr auto extract_signature(R (*const)(A...)) noexcept
{
return extract_signature1<R, A...>();
}

template <typename F>
constexpr auto extract_signature(F const& f) noexcept ->
decltype(&F::operator(), extract_signature2(&F::operator()))
{
return extract_signature2(&F::operator());
}

int main()
{
auto lambda = [](int, int, int){};
std::cout << ::std::integral_constant<::std::size_t, decltype(extract_signature(lambda))::count>{} << ::std::endl;
}

关于c++ - 提取 "invokables"的签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36493935/

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