了解此高度模板化的C ++函数绑定器 [英] Understanding this highly templated C++ function binder

查看:35
本文介绍了了解此高度模板化的C ++函数绑定器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  template< typename容器,typename Ret,typename ... Args> 
struct BindImpl {
template< Ret(Container :: * MemberFunc)(Args ...)>
类Callable {
public:
inline constexpr Callable(容器* container):
m_container(容器)
{}

内联Ret operator()(Args ... args)const
{
return(m_container-> * MemberFunc)(std :: forward< Args>(args)...);
}

内联函数< Ret(Args ...)> toFunction()const
{
return Function< Ret(Args ...)>(* this);
}

私人:
容器* m_container;
};
};
template< typename容器,类型名Ret,类型名... Args>
BindImpl< Container,Ret,Args ...> DeduceImpl(Ret(Container :: *)(Args ...));

此代码的调用方式如下:

 (类型名decltype(:: AIpStack :: BindPrivate :: DeduceImpl(& EthIpIface :: driverSendIp4Packet))):: template Callable<& EthIpIface :: driverSendIp4Packet>((this))。 toFunction())

我试图了解这段代码的作用。适当地,它是一种将函数指针(例如& EthIpIface :: driverSendIp4Packet )绑定到某种方式的方法。



上面的行来自此宏,其中填充此结构成员(如果有人对此感兴趣)。您可能想在Function处有战利品



我不理解的第一部分是

  template< Ret(Container :: * MemberFunc)(Args ...)> 

对我来说,必须先跟随模板 typename 。而且,紧随类型名之后的是要替换的东西。我看不到此模板如何使Callable模板化。我不知道 Callable< something> 中的东西去哪里。



另外, DeduceImpl 是什么?



另外, Container :: * MemberFunc 是什么意思?

p>

解决方案

首先,模板还可以接受非类型参数,以及类型名和类。在这种情况下:

  template< Ret(Container :: * MemberFunc)(Args ...)> 

这是一个以函数指针为参数的模板,其中 Ret 是返回类型, Container :: * MemberFunc 是指向 Container 带有 Args ... 引用可变参数。这为指针提供了标识符 MemberFunc 。范围解析运算符使您感到困惑,因为在任何其他情况下同时使用这两种方法但在此特定情况下 这两个被视为一个令牌 :: * 代表这种模板参数,而不是两个 :: * 。 / p>




为此行:

  BindImpl< Container,Ret,Args ...> DeduceImpl(Ret(Container :: *)(Args ...)); 

这是一个函数声明。这是一个名为 DeduceImpl 的函数,该函数将返回一个以函数指针作为参数的 BindImpl 结构。我推断此函数是绑定函数指针的接口,因此,据我所读,(大概)简称为 Deduce Implementation和 Bind Implementation,该函数仅用于decltype,因此






有关此模板在此行中的实际使用方式(重新格式化以便于阅读) ):

  typename decltype(:: AIpStack :: BindPrivate :: DeduceImpl(& EthIpIface :: driverSendIp4Packet))
::
模板Callable<& EthIpIface :: driverSendIp4Packet>(this).toFunction()

这翻译成意思(或多或少):



对于特定类型的 BindImpl 将保存指向 driverSendIp4Packet()的函数指针,创建一个显式实例化 Callable 成员函数 toFunction()



只是基本上说您正在使用运行<$ c的显式实例 toFunction() $ c> driverSendIp4Packet()



您不会只使用模板就编写所有这些内容。编写此行很可能是因为这是在项目中实例化模板的几种方法之一。






总结:




  • template< Ret(Container :: * MemberFunc)(Args ...)> 是一个模板,该模板使用称为 MemberFunc 的函数指针作为参数。

  • DeduceImpl 通过接收要绑定的函数指针来返回 BindImpl 结构。


template<typename Container, typename Ret, typename ...Args>
    struct BindImpl {
        template<Ret (Container::*MemberFunc)(Args...)>
        class Callable {
        public:
            inline constexpr Callable (Container *container) :
                m_container(container)
            {}

            inline Ret operator() (Args ...args) const
            {
                return (m_container->*MemberFunc)(std::forward<Args>(args)...);
            }

            inline Function<Ret(Args...)> toFunction() const
            {
                return Function<Ret(Args...)>(*this);
            }

        private:
            Container *m_container;
        };
    };
    template<typename Container, typename Ret, typename ...Args>
    BindImpl<Container, Ret, Args...> DeduceImpl (Ret (Container::*)(Args...));

This code is called like this:

(typename decltype(::AIpStack::BindPrivate::DeduceImpl(&EthIpIface::driverSendIp4Packet)) ::template Callable<&EthIpIface::driverSendIp4Packet>((this)).toFunction())

I'm trying to understand what this code does. It apprently is a way to bind function pointers (like &EthIpIface::driverSendIp4Packet) to something.

The line above is from this macro, which fills this struct member, if anyone is intersted. You may wanna have a loot at Function.

The first part that I don't understand is

template<Ret (Container::*MemberFunc)(Args...)>

For me a template must be followed by typename. Also, what follows typename, is the thing to be substituted for. I don't see how this template makes Callable templated. I don't know where something goes to in Callable<something>.

Also, what is DeduceImpl? Looks like a function declaration but without a definition.

Also, what Container::*MemberFunc means?

解决方案

Firstly, templates can also take in non-type parameters as well as with typename and class. In this case:

template<Ret (Container::*MemberFunc)(Args...)>

This is a template taking a function pointer as a parameter, where Ret is the return type, Container::*MemberFunc is the pointer to a specific member function in Container with Args... referencing variadic arguments. This gives the pointer the identifier MemberFunc. I have a feeling the asterisk following the scope resolution operator confused you, as usually you would receive a compiler error if you used these two together in any other situation but in this specific case these two are considered one token ::* representing this kind of template parameter instead of the two :: and *.


For this line:

BindImpl<Container, Ret, Args...> DeduceImpl (Ret (Container::*)(Args...));

It is a function declaration. This is a function named DeduceImpl that will return a BindImpl struct that takes a function pointer as an argument. I'm inferring that this function is the interface by which you bind the function pointer, hence the (probably) shortened names "Deduce Implementation" and "Bind Implementation" From what I've read, this function is only used for decltype, so there's no actual definition for this function.


For how this template is actually being utilized in this line (reformatted for easier reading):

typename decltype(::AIpStack::BindPrivate::DeduceImpl(&EthIpIface::driverSendIp4Packet))
::
template Callable<&EthIpIface::driverSendIp4Packet>(this).toFunction()

This translates to meaning (more or less):

For the specific kind of BindImpl that would hold the function pointer to driverSendIp4Packet(), create an explicit instantiation of the Callable member function toFunction().

It's a mouthful just to basically say you're using an explicit instantiation of toFunction() that runs driverSendIp4Packet().

You wouldn't write all of this just to use the template. This line was probably written because it's one of the few ways the template is instantiated in the project.


In summary:

  • template<Ret (Container::*MemberFunc)(Args...)> is a template that takes a function pointer referred to as MemberFunc as a parameter.
  • DeduceImpl returns a BindImpl struct by taking in the function pointer you want to bind.

这篇关于了解此高度模板化的C ++函数绑定器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆