简体   繁体   English

C ++模板中的函数签名

[英]Function signatures in C++ templates

The std::function class is templated in such a way that when we want it to wrap a function like the following: std::function类以这样的方式进行模板化,当我们希望它包装一个如下所示的函数时:

void printInt(int integer)
{
    std::cout << int << '\n';
}

We use a std::function<void(int)> . 我们使用std::function<void(int)> Until recently I thought this was an odd nuance of the class, but a class I found while searching for delegate implementation in C++ uses a similar syntax. 直到最近我才认为这是该类的一个奇怪的细微差别,但在C ++中搜索委托实现时发现的类使用了类似的语法。

What exactly is void(int) , and what do we call it in technical terms? 究竟什么 void(int) ,我们在技术术语中称之为什么? It seems to be the standard way of saying " a function that takes an int, and returns void " in codespeak, but my gut instinct says that's horribly oversimplified. 它似乎是在代码语句中说“ 一个接受int,并返回void的函数 ”的标准方式,但我的直觉本能说它过于简单了。

Secondly, I've noticed that when I see templates using this syntax they use variadic templates to allow multiple function signatures to be matched. 其次,我注意到当我看到使用这种语法的模板时,他们使用可变参数模板来匹配多个函数签名。 From the link above: 从上面的链接:

template <typename T> class delegate;

template<class R, class ...A>
class delegate<R (A...)>
{
...

What is the reason for declaring the function as such instead of simply using the following: 声明函数的原因是什么,而不是简单地使用以下内容:

template<class R, class ...A>
class delegate
{
...

The template parameter to std::function<Signature> is simply the type of a function, ie, its signature. std::function<Signature>的模板参数只是函数的类型,即其签名。 It uses the same notation as any function declaration except that it isn't named and the name is left out. 它使用与任何函数声明相同的表示法,除了它没有命名并且省略了名称。 You may have come across function pointers which use the same notation but the function signature is used for a pointer. 您可能遇到过使用相同表示法的函数指针,但函数签名用于指针。

The reason std::function<Signature> (and apparently delegate<Signature> ) are implemented using template specialization is to yield a nicer type: 使用模板特化实现std::function<Signature> (以及显然delegate<Signature> )的原因是为了产生更好的类型:

template <typename T> class function;
template <typename R, typename... Args>
class function {
public:
    R operator()(Args...);
    // ...
};

template <typename R, typename... Args>
class other {
public:
    R operator()(Args...);
    // ...
};

int main() {
    function<int(double, char)> f;
    other<int, double, char>    o;
}

Since the primary template for function<T> takes one type as argument, using the specialization the argument can be a normal function type. 由于function<T>的主模板采用一种类型作为参数,因此使用特化参数可以是普通函数类型。 On the other hand, the same isn't done for other<T...> which, thus, gets a list of types. 另一方面,对于other<T...>没有做同样的事情,因此获得了类型列表。

It is worth nothing that std::function<T> objects can be passed around quite easily without any need to deal with many template arguments: since the function's signature is just a type, this class template takes just one template argument. 值得一提的是std::function<T>对象可以很容易地传递而不需要处理许多模板参数:因为函数的签名只是一个类型,所以这个类模板只需要一个模板参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM