简体   繁体   English

什么时候应该在C ++中使用函数而不是函子?

[英]When should you ever use functions over functors in C++?

Functors are apparently more efficient since the compiler is more easily able to inline them, and they work much better with parametrization. 由于编译器更容易内联它们,因此函数显然更有效,并且它们在参数化方面工作得更好。 When should you ever use a plain old function over a functor? 什么时候应该使用普通的旧函数而不是函子?

Functions support distributed overriding. 函数支持分布式覆盖。 Functors do not. 函数没有。 You must define all of the overloads of a Functor within itself; 您必须在其自身内定义Functor的所有重载; you can add new overloads of a function anywhere . 您可以在任何地方添加函数的新重载。

Functions support ADL (argument dependent lookup), permitting overloading in the argument-type associated namespace. 函数支持ADL(依赖于参数的查找),允许在参数类型关联的命名空间中进行重载。 Functors do not. 函数没有。

Function pointers are (kind of) a type-erased stateless functor that is a POD, as evidenced by how stateless lambdas convert into it. 函数指针(类型)是一种类型擦除的无状态函子,它是一个POD,正如无状态lambdas如何转换成它一样。 Such features (POD, stateless, type erasure) are useful. 这些特征(POD,无状态,类型擦除)很有用。

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

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