VC中function函数解析
C++标准库是日常应用中非常重要的库,我们会用到C++标准库的很多组件,C++标准库的作用,不单单是一种可以很方便使用的组件,也是我们学习很多实现技巧的重要宝库。我一直对C++很多组件的实现拥有比较强的兴趣。最近花了一些时间,查看了C++中function类的实现,将其中的要点,写在这里(这里只介绍其中的一部分):
1.首先VC实现了将<Ret(T1, T2, ...)>这种类型的类型参数,改变为<Ret, T1, T2, ...>这种类型的类型参数。使用的方法如下:
template <class _Fty>
class function : public _Get_function_impl<_Fty>::type
{
public:
using _Mybase = _Get_function_impl<_Fty>::type;
public:
function() noexcept
{ // construct empty function wrapper
} template <class _Fx,
class = typename _Mybase::_Enable_if_callable_t<_Fx&, function>>
function(_Fx _Func)
{ // construct wrapper holding copy of _Func
this->_Reset(std::move(_Func));
} private:
// 没有其他的数据成员
};
不过,对于_Get_function_impl<_Fty>::type的实现,应该是编译器额外处理(我不记得C++中有类似的语法),大致处理如下(如下代码不能编译通过):
template <class _Ret,
class... _Types>
struct _Get_function_impl<_Ret CALL_OPT (_Types...)>
{
using type = _Func_class<_Ret, _Types...>;
};
参考以上代码,也可以使用boost中的boost::typeindex,可以知道,function的实现,继承于_Func_class,function的实现,使用了类似于Adapter的方式,具体的实现细节在_Func_class中。这点,可以参考上面的function构造函数。function中没有直接定义operator()函数,operator()函数在_Func_class中定义,我们查看一下_Func_class函数:
using max_align_t = double; // most aligned type
// size in pointers of std::function and std::any (roughly 3 pointers larger than std::string when building debug
constexpr int _Small_object_num_ptrs = + / sizeof(void *);
constexpr size_t _Space_size = (_Small_object_num_ptrs - ) * sizeof(void*); template <class _Ret,
class... _Types>
class _Func_class
{ // implement function template
public:
using result_type = _Ret; using _Ptrt = _Func_base<_Ret, _Types...>; _Func_class() noexcept
{ // construct without stored object
_Set();
} _Ret operator()(_Types... _Args) const
{
if (_Empty())
{
_Xbad_function_call();
}
const auto _Impl = _Getimpl();
return (_Impl->_Do_call(_STD forward<_Types>(Args)...));
} protected:
// 用于判断传入的函数对象可以调用_Types...表示的参数,并且返回_Ret类型的参数,
// 而且不为function类型(至少上面的调用时是这个意思)
template<class _Fx,
class _Function>
using _Enable_if_callable_t = enable_if_t<conjunction_v<negation<is_same<decay_t<_Fx>, _Function>>,
_Is_invocable_r<_Ret, _Fx, _Types...>>>; bool _Empty() const _NOEXCEPT
{ // return true if no stored object
return (_Getimpl() == );
} template <class _Fx>
void _Reset(_Fx&& _Val)
{ // store copy of _Val
if (!_Test_callable(_Val))
{ // null member pointer/function pointer/std::function
return; // already empty
} using _Impl = _Func_impl_no_alloc<decay_t<_Fx>, _Ret, _Types...>;
_Reset_impl<_Impl>(std::forward<_Fx>(_Val), _Is_large<_Impl>());
} template <class _Myimpl, class _Fx>
void _Reset_impl(_Fx&& _Val, true_type)
{ // store copy of _Val, large (dynamically allocated)
_Set(_Global_new<_Myimpl>(std::foward<_Fx>(_Val)));
} template <class _Myimpl, class _Fx>
void _Reset_impl(_Fx&& _Val, false_type)
{ // store copy of _Val, small (locally stored)
// placement operator new,将对象创建在_Mystorage所在的地址
_Set(::new (_Getspace()) _Myimpl(std::forward<_Fx>(_Val)));
} void _Tidy() noexcept
{ // clean up
if (!_Empty())
{ // destroy callable object and maybe delete it
_Getimpl()->_Delete_this(!_Local());
_Set();
}
} private:
union _Storage
{ // storage for small objects (basic_string is small)
max_align_t _Dummy1; // for maximum alignment
char _Dummy2[_Space_size]; // to permit aliasing
_Ptrt *_Ptrs[_Small_object_num_ptrs]; // _Ptrs[_Small_object_num_ptrs - 1] is reserved
}; _Storage _Mystorage; // 数据成员 bool _Local() const noexcept
{ // test for locally stored copy of object
return (_Getimpl() == _Getspace());
} _Ptrt* _Getimpl() const noexcept
{ // get pointer to object
return (_Mystorage._Ptrs[_Small_object_num_ptrs - ]);
} void _Set(_Ptrt* _Ptr) noexcept
{ // store pointer to object
_Mystorage._Ptrs[_Small_object_num_ptrs - ] = _Ptr;
} void *_Getspace() noexcept
{ // get pointer to storage space
return (&_Mystorage);
} const void* _Getspace() const noexcept
{
return (&_Mystorage);
}
};
这个类只有一个数据成员,就是_Storage _Mystorage;其中_Storage是个union,union中两个成员用了Dummy开头,dummy的意思,就是只是为了实现某些目的,实际中并不会应用,_Dummy1的目的是为了让这个对象最大对齐,_Dummy2说是用于别名,我从实现来看,更像用来表示多大的函数对象可以本地存储,_Space_size表示的就是可以将函数对象直接存储到_Func_class中最大的值,而_Ptrs[_Small_object_num_ptrs]的大小,刚好比_Dummy2对一个指针的长度大小,因为最后一个指针需要用了存储实际函数的起始地址,这点,可以参考_Getimpl函数和_Set函数。这样做的目的,应该是减少new的次数,因为new的次数过多,容易导致比较严重的碎片化,而且new本来速度也比不了在堆栈中分配内存的速度,不过,另一方面,从实现来看,一个function占用的大小,远大于一个函数指针的大小,更远大于一个没有数据成员的函数对象的大小。如果对于内存占用有很大的要求,而且需要的函数对象又特别多,例如附带函数的事件处理队列等,需要慎重考虑一下。
说了这么多,那么C++中是如何做到在函数对象比较小的情况下,将函数对象存储到本地,而比较大的时候,将函数对象在堆上分配呢?我们需要查看一下:
_Ptrt *_Ptrs[_Small_object_num_ptrs];中的_Ptrt,也就是_Func_base:
template <class _Rx,
class... _Types>
class _Func_base
{ // abstract base for implementation types
public:
virtual _Func_base* _Copy(void*) const = ;
virtual _Func_base* _Move(void*) const = ;
virtual _Fx _Do_call(Types&&...) = ;
virtual void _Delete_this(bool) _NOEXCEPT = ; _Func_base() = default;
_Func_base(const _Func_base&) = delete;
_Func_base& operator=(const _Func_base&) = delete;
// destructor non-virtual due to _Delete_this()
}; template <class _Callable,
class _Rx,
class... _Types>
class _Func_impl_no_alloc final : public _Func_base<_Rx, _Types...>
{ // derived class for specific implementation types that don't use allocators
public:
using _Mybase = _Func_base<_Rx, _Types...>;
using _Nothrow_move = is_nothrow_move_constructible<_Callable>; template <class _Other,
class = enable_if_t<!is_same_v<_Func_impl_no_alloc, decay_t<_Other>>>>
explicit _Func_impl_no_alloc(_Other&& _Val)
: _Callee(std::forward<_Other>(_Val))
{ // construct
} private:
virtual _Mybase *_Copy(void *_Where) const override
{ // return clone of *this
return (_Clone(_Where, _Is_large<_Func_impl_no_alloc>()));
} _Mybase *_Clone(void *, true_type) const
{ // return clone of *this, large (dynamically allocated)
return (_Global_new<_Func_impl_no_alloc>(_Callee));
} _Mybase *_Clone(void *_Where, false_type) const
{ // return clone of *this, small (locally stored)
return (::new (_Where) _Func_impl_no_alloc(_Callee));
} virtual _Mybase *_Move(void *_Where) override
{ // return clone of *this
return (::new (_Where) _Func_impl_no_alloc(std::move(_Callee)));
} virtual _Rx _Do_call(_Types&&... _Args) override
{
return (_Invoker_ret<_Rx>::_Call(_Callee, std::forward<_Types>(_Args)...));
} virtual const void *_Get() const noexcept override
{ // return address of stored object
return (std::addressof(_Callee));
} virtual void _Delete_this(bool _Dealloc) noexcept override
{
this->~_Func_impl_no_alloc();
if (_Dealloc)
{
_Deallocate<alignof(_Func_impl_no_alloc)>(this, sizeof(_Func_impl_no_alloc));
}
} _Callable _Callee;
};
我们认真查看上述代码,不难发现,区分在_Copy函数中的_Is_large<_Func_impl_no_alloc>(),实际调用是:_Mybase *_Clone(void *, true_type),采用的是在堆中分配一段空间,而_Mybase *_Clone(void *_Where, false_type)采用的是placement operator new,将对象创建在本地。我们查看_Func_class中的Reset系列函数,也可以发现_Is_large的使用,以及堆栈分配和本地分配的区别。下面,给出_Is_large的实现:
template <class _Impl>
struct _Is_large
: bool_constant<_Space_size < sizeof(_Impl)
|| !_Impl::_Nothrow_move::value>
{ // determine whether _Impl must be dynamically allocated
};
其中true_type是bool_constant<true>,而false_type是bool_constant<false>,为不同的类型。最后,还要要简单提及一下的是:_Func_class是继承于public _Arg_types<_Types...>,而
// 这个类的目的是提供argument_type, first_argument_type, second_argument_type,
// 因为C++17中已经为deprecated,所以,没有查看的必要
template <class... _Types>
struct _Arg_types
{ // provide argument_type, etc. (sometimes)
};
所以,我从简单考虑,之前没有写出这种继承关系,对实际理解应该没有什么影响。上述,就是我的简单介绍。
VC中function函数解析的更多相关文章
- 转:VC中UpdateData()函数的使用
VC中UpdateData()函数的使用 UpdateData(FALSE)与UpdateData(TRUE)是相反的过程 UpdateData(FALSE)是把程序中改变的值更新到控件中去 ...
- 2019-2-14SQLserver中function函数和存储过程、触发器、CURSOR
Sqlserver 自定义函数 Function使用介绍 前言: 在SQL server中不仅可以可以使用系统自带的函数(时间函数.聚合函数.字符串函数等等),还可以根据需要自定义函数 ...
- hive中function函数查询
1. desc function [函数名] desc function xpath; 查询用法: 2. desc function extended [函数名] desc function exte ...
- JavaScript中Function函数与Object对象的关系
函数对象和其他内部对象的关系 除了函数对象,还有很多内部对象,比如:Object.Array.Date.RegExp.Math.Error.这些名称实际上表示一个 类型,可以通过new操作符返回一个对 ...
- js中function函数
function:是具备某个功能的方法,方法本身没有意义,只有执行方法才有价值. function: 1 创建一个函数: 2 执行这个方法: 例: 创建 function 方法名(){ 存放某个功能的 ...
- java中main函数解析(转载)
从写java至今,写的最多的可能就是主函数 public static void main(String[] args) {} 但是以前一直都没有问自己,为什么要这么写,因为在c语言中就没有这样子的要 ...
- Python中function(函数)和methon(方法)的区别
在Python中,对这两个东西有明确的规定: 函数function —— A series of statements which returns some value to a caller. It ...
- php中parse_url函数解析
1.在php开发过程中我们经常要用到用户上传文件这个功能,那么用户上传文件我们肯定要知道用户上传文件的合法性,那么我们就要从url中获取文件的扩展名.那么就会用到parse_url()这个函数. pa ...
- java中main函数解析
从写java至今,写的最多的可能就是主函数 public static void main(String[] args) {} 但是以前一直都没有问自己,为什么要这么写,因为在c语言中就没有这样子的要 ...
随机推荐
- golang pprof 内存分析
use pprof to get application memory useage add code in your main funciton import ( "log" ...
- Microsoft Bot Framework 上手
因为这前使用过MS Bot Frameowrk 做过开发, 最近心血来潮想做(挖坑)一个小的bot. 今天带领大家使用MS Bot Framework创建Hello World. 首先,我们要创建项目 ...
- Python time & datetime模块
time 模块 时间分为三种格式: 时间戳:表示1970年1月1日之后的秒 结构化时间:元组包含了:年.日.星期等... 格式化字符串:格式可以自定义 时间戳: import time time_st ...
- 关于 php 和 python 的浮点计算 0.1+0.2
关于 php 和 python 的浮点计算 0.1+0.2 看到群里有小伙伴说为什么 python 计算出 0.1+0.2 是 0.30000000000000004 >>> pri ...
- [转载] IIS来搭建一个只能实现基本功能的FTP服务器
转自 http://blog.sina.com.cn/s/blog_3f7e47f20100haur.html 本文介绍通过win7自带的IIS来搭建一个只能实现基本功能的FTP服务器,第一次装好W ...
- redis设计与实现-数据结构
1,redis存储有5种数据对象,有7种数据结构底层实现 2,sds简单字符串 不直接使用字符数组或是string 封装了长度变量,加快获得字符串长度 杜绝缓冲区溢出(拼接字符串的时候不会因为内存里连 ...
- HTTP 状态码的完整列表
一.1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态码.SC_CONTINUE = 100; 100(继续)请求者应当继续提出请求.服务器返回此代码表示已收到请求的第一部分,正在等待其余 ...
- lsof根据端口返回进程号杀死进程的方法
参考自:http://newmiracle.cn/?p=661 Linux shell根据端口返回进程号杀死进程的方法 kill -9 `lsof -t -i:8888` 这个就是杀死8888端口的进 ...
- ecstore-ftp设置,不能上传文件
某些主机居然不能上传,ftp改成127.0.0.1即可 ftp地址改成127.0.0.1即可...
- Swoole来实现实时异步任务队列
假如要发100封邮件,for循环100遍,用户直接揭竿而起,什么破网站!但实际上,我们很可能有超过1万的邮件.怎么处理这个延迟的问题?答案就是用异步.把“发邮件”这个操作封装,然后后台异步地执行1万遍 ...