简体   繁体   English

如何通过将重载函数的签名作为模板参数来解析重载函数?

[英]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.

相关问题 如何将任意类的任意成员函数传递给模板以解析其签名? - How do I pass arbitrary member function of ANY class to template in order to resolve its signature? 模棱两可的重载函数仅因参数的模板参数而异 - ambiguous overloaded function differs only by argument's template parameter 函数签名作为模板参数 - Function signature as template parameter 如何从父类的函数调用子代的重载运算符,该函数以对父代的引用作为参数? - How do I call a child's overloaded operator from a function in the parent class which has a reference to the parent as a parameter? 我该如何解决这个问题<unresolved overloaded function type>使用 std::function 时出错?</unresolved> - How do I resolve this <unresolved overloaded function type> error when using std::function? 声明与给定模板参数的功能具有相同签名的功能 - Declaring a function with same signature of the given template parameter's function 模板类型推导如何使用重载函数作为参数 - How does template type deduction work with an overloaded function as the parameter 功能签名作为功能模板参数 - Function signature as a function template parameter 重载的运算符将函数指针作为参数,我该如何检索函数指针的参数 - An overloaded operator takes function pointer as parameter, how do i retrieve arguments of function pointer 我该如何解决该成员函数问题重载的地址? - How can I resolve this address of overloaded member function issue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM