在C ++ 11中使用函数指针 [英] using function pointers in C++ 11

查看:145
本文介绍了在C ++ 11中使用函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数指针的主要原因是提供通用的可调用对象,但是在C ++ 11中,提供它的方法是使用 std :: function 任何理由在现代C ++中使用函数指针,除了与C的兼容性?换句话说,是 std :: function 一个现代版本的函数指针,就像 std :: array vs built -in arrays?

The main reason for function pointers is to provide generic callable objects, but in C++ 11 the way to provide it would be to use std::function, so is there any reason to use function pointers in modern C++ apart from compatibility with C? In other words, is std::function a modern version of function pointers, exactly like std::array vs built-in arrays?

推荐答案

是的,我更喜欢 std :: function 几乎无处不在,但是在少数情况下, std :: function 的大小开销和间接可能是不可接受的。对于包含在 std :: function 中的小型可调用函数指针,通常没有额外的间接和动态内存分配,因为可调用符合 std :: function 但是 std :: function 可能很大以容纳更大的可调用,所以可能会有很大的空间开销。

Yes I'd prefer std::function nearly everywhere but there are few cases where size overhead and indirection of std::function may not be acceptable. For small callables like function pointers wrapped in std::function, there is typically no additional indirection and no dynamic memory allocation since the callable fits into the std::function but std::function may be big to hold even bigger callables so there may be a big space overhead.

这篇关于在C ++ 11中使用函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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