[英]How do I resolve an overloaded function by it's signature as a template parameter?
I'm writing a delegate library and stubled upon this problem: Let's say I have overloaded functions named foo like this:我正在编写一个委托库并在这个问题上遇到了麻烦:假设我重载了名为 foo 的函数,如下所示:
int foo(double d);
double foo(int d);
How would I write my template argument list if I want to resolve which function is meant by specifying a signature as a template parameter.如果我想通过将签名指定为模板参数来解析哪个函数的含义,我将如何编写模板参数列表。 I basically want this syntax (yet it shall work with any signature):我基本上想要这种语法(但它适用于任何签名):
Delegate d = make_delegate<&foo,int(double)>(); // "Delegate" is automatically deduced as Delegate<int(double)>
I managed to resolve it by using the following helper template, but it only works if I write the parameter types of the function signature manually.我设法通过使用以下帮助器模板解决了它,但它仅在我手动编写函数签名的参数类型时才有效。 I struggle to forward the variadic parameter pack Args...
(which is encoded in the Args_pack specialization) to the function signature.我努力将可变参数包Args...
(在 Args_pack 专业化中编码)转发到函数签名。
template<typename... Args>
struct Args_pack {}
template<typename Signature>
struct Identify_signature_type;
template<typename Return, typename... Args>
struct Identify_signature_type<Return(Args...)> {
using T_return = Return;
using Args_pack = Args_pack<Args...>;
};
template<auto Signature> using Identify_signature = Identify_signature_type<decltype(Signature)>;
template<typename Signature, typename Identify_signature_type<Signature>::T_return Function(double /* important magic needed here */)>
auto make_delegate()
{...}
Delegate d = make_delegate<int(double), &foo>(); // Works. However, it would be nice if the template parameters could exchange places.
You can add a *
to the signature to get the right function pointer type.您可以在签名中添加*
以获得正确的函数指针类型。
template<typename Signature, Signature* fptr>
auto make_delegate()
{...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.