VC的function类说明 -- 继续
我在之前的随笔中介绍了function如何保存参数,如何实现调用相关知识。对于一个函数对象或者函数指针来说,应该很容易理解。不过对于如何在function中保存类的成员函数,这个还是值得一说的。
还是按照之前的方式,通过boost的type_index,我们可以比较容易的知道function的父类是_Func_class。
这里先看一段代码:
template<class _Fx,
class _Inv_res = typename _Mybase::template _Result_of_invoking_t<_Fx&>,
class = typename _Mybase::template _Enable_if_returnable_t<_Inv_res> >
function(_Fx _Func)
{ // construct wrapper holding copy of _Func
this->_Reset(_STD move(_Func));
}
这个是function的一个构造函数,其中的_Inv_res是这个构造函数能够使用的条件,条件的内容是_Mybase::_Result_of_invoking_t<_Fx&>可以获得一个类型。
现在我们查看_Result_of_invoking_t定义的位置:
protected:
template<class _Fx>
using _Result_of_invoking_t = result_of_t<_Fx(_Types...)>;
上面是在_Func_class中的定义。下面给出类result_of_t的定义。
template<class _Ty>
using result_of_t = typename result_of<_Ty>::type; template<class _Void,
class... _Types>
struct _Result_of
{ // selected when _Fty isn't callable with _Args
}; template<class... _Types>
struct _Result_of<
void_t<
_Unique_tag_result_of, // TRANSITION, C1XX
decltype(_STD invoke(_STD declval<_Types>()...))>,
_Types...>
{ // selected when _Fty is callable with _Args
typedef decltype(_STD invoke(_STD declval<_Types>()...)) type;
}; template<class _Fty>
struct result_of
{ // explain usage
static_assert(_Always_false<_Fty>::value,
"result_of<CallableType> is invalid; use "
"result_of<CallableType(zero or more argument types)> instead.");
}; #define _RESULT_OF(CALL_OPT, X1, X2) \
template<class _Fty, \
class... _Args> \
struct result_of<_Fty CALL_OPT (_Args...)> \
: _Result_of<void, _Fty, _Args...> \
{ /* template to determine result of call operation */ \
};
由上述代码可知,result_of的实现依赖于std::invoke函数的实现。
我们再查看一下function函数的调用路径:
_Ret operator()(_Types... _Args) const
{ // call through stored object
if (_Empty())
_Xbad_function_call();
return (_Getimpl()->_Do_call(_STD forward<_Types>(_Args)...));
}
上述是_Func_class的调用函数。
virtual _Rx _Do_call(_Types&&... _Args)
{ // call wrapped function
return (_Invoke_ret(_Forced<_Rx>(), _Callee(),
_STD forward<_Types>(_Args)...));
}
这个是_Func_impl的调用,也就是上面_Getimpl()->_Do_call的实现函数。下面,我们再查看一下_Invoke_ret的实现:
template<class _Cv_void,
class... _Valtys> inline
void _Invoke_ret(_Forced<_Cv_void, true>, _Valtys&&... _Vals)
{ // INVOKE, "implicitly" converted to void
_STD invoke(_STD forward<_Valtys>(_Vals)...);
} template<class _Rx,
class... _Valtys> inline
_Rx _Invoke_ret(_Forced<_Rx, false>, _Valtys&&... _Vals)
{ // INVOKE, implicitly converted to _Rx
return (_STD invoke(_STD forward<_Valtys>(_Vals)...));
} template<class... _Valtys> inline
auto _Invoke_ret(_Forced<_Unforced, false>, _Valtys&&... _Vals)
-> decltype(_STD invoke(_STD forward<_Valtys>(_Vals)...))
{ // INVOKE, unchanged
return (_STD invoke(_STD forward<_Valtys>(_Vals)...));
}
由上面代码可见,无一例外,function的判断和实现都依赖于std::invoke的实现。那么std::invoke是如何实现的呢?
template<class _Callable,
class... _Types> inline
auto invoke(_Callable&& _Obj, _Types&&... _Args)
-> decltype(_Invoker<_Callable, _Types...>::_Call(
_STD forward<_Callable>(_Obj), _STD forward<_Types>(_Args)...))
{ // INVOKE a callable object
return (_Invoker<_Callable, _Types...>::_Call(
_STD forward<_Callable>(_Obj), _STD forward<_Types>(_Args)...));
}
在VS2015中,invoke的实现代码如上,可见invoke的实现依赖于_Invoker类。下面,我们查看一下_Invoker的实现:
template<class _Callable,
class... _Types>
struct _Invoker; template<class _Callable>
struct _Invoker<_Callable>
: _Invoker_functor
{ // zero arguments
}; template<class _Callable,
class _Ty1,
class... _Types2>
struct _Invoker<_Callable, _Ty1, _Types2...>
: _Invoker1<_Callable, _Ty1>
{ // one or more arguments
};
可见,我们需要继续查看_Invoker1的实现:
template<class _Callable,
class _Ty1,
class _Decayed = typename decay<_Callable>::type,
bool _Is_pmf = is_member_function_pointer<_Decayed>::value,
bool _Is_pmd = is_member_object_pointer<_Decayed>::value>
struct _Invoker1; template<class _Callable,
class _Ty1,
class _Decayed>
struct _Invoker1<_Callable, _Ty1, _Decayed, true, false>
: _If<is_base_of<
typename _Is_memfunptr<_Decayed>::_Class_type,
typename decay<_Ty1>::type>::value,
_Invoker_pmf_object,
_Invoker_pmf_pointer>::type
{ // pointer to member function
}; template<class _Callable,
class _Ty1,
class _Decayed>
struct _Invoker1<_Callable, _Ty1, _Decayed, false, true>
: _If<is_base_of<
typename _Is_member_object_pointer<_Decayed>::_Class_type,
typename decay<_Ty1>::type>::value,
_Invoker_pmd_object,
_Invoker_pmd_pointer>::type
{ // pointer to member data
}; template<class _Callable,
class _Ty1,
class _Decayed>
struct _Invoker1<_Callable, _Ty1, _Decayed, false, false>
: _Invoker_functor
{ // function object
};
以及实现_Invoker1的底层类:
struct _Invoker_pmf_object
{ // INVOKE a pointer to member function on an object
template <class _Decayed, class _Ty1, class... _Types2>
static auto _Call(_Decayed _Pmf, _Ty1&& _Arg1, _Types2&&... _Args)
->decltype((std::forward<_Ty1>(_Arg1).*_Pmf)(
std::forward<_Types2>(_Args2)...))
{ // INVOKE a pointer to member function on an object
return ((_STD forward<_Ty1>(_Arg1).*_Pmf)(
std::forward<_Types2>(_Args2)...
));
}
}; struct _Invoker_pmf_pointer
{ // INVOKE a pointer to member function on a [smart] pointer
template <class _Decayed, class _Ty1, class... _Types2>
static auto _Call(_Decayed _Pmf, _Ty1&& _Arg1, _Types2&&... Args2)
->decltype(((*std::forward<_Ty1>(_Arg1)).*_Pmf)(
std::forward<_Types2>(_Arg2)...))
{ // INVOKE a pointer to member function on a [smart] pointer
return (((*std::forward<_Ty1>(_Arg1)).*_Pmf)(
std::forward<_Types2>(_Arg2)...));
}
}; struct _Invoker_pmd_object
{ // INVOKE a pointer to member data on an object
template<class _Decayed, class _Ty1>
static auto _Call(_Decayed _Pmd, _Ty1&& _Arg1)
->decltype(std::forward<_Ty1>(_Arg1).*_Pmd)
{ // INVOKE a pointer to member data on a [smart] pointer
return (std::forward<_Ty1>(_Arg1).*_Pmd);
} }; struct _Invoker_pmd_pointer
{ // INVOKE a pointer to member data on a [smart] pointer
template <class _Decayed, class _Ty1>
static auto _Call(_Decayed _Pmd, _Ty1&& _Arg1)
->decltype((*std::forward<_Ty1>(_Arg1)).*_Pmd)
{ // INVOKE a pointer to member data on a [smart] pointer
return ((*std::forward<_Ty1>(_Arg1)).*_Pmd);
}
}; struct _Invoker_functor
{ // INVOKE a function object
template <class _Callable, class... _Types>
static auto _Call(_Callable&& _Obj, _Types&&... _Args)
->decltype(std::forward<_Callable>(_Obj)(
std::forward<_Types>(_Args)...))
{ // INVOKE a function object
return (std::forward<_Callable>(_Obj)(
std::forward<_Types>(_Args)...));
}
};
实现的过程,主要在于:bool _Is_pmf = is_member_function_pointer<_Decayed>::value和bool _Is_pmd = is_member_object_pointer<_Decayed>::value>两个判断语句,通过这个来实现SFINAE的语义,从而实现针对类型的特例化。为了说明上面两个判断才是重点,输入如下代码:
class A
{
public:
A(){} void printA() const
{
std::cout << "printA" << std::endl;
}
}; void printB(A a)
{
std::cout << "printB" << std::endl;
} int main()
{
std::_Invoker_pmf_object::_Call(&A::printA, A());
std::_Invoker_pmf_pointer::_Call(&A::printA, &A());
std::_Invoker_functor::_Call(printB, A()); return ;
}
查看打印结果。由于_Invoker_pmf_object,_Invoker_pmf_pointer和_Invoker_functor的实现本来很简单,所以invoke调用的重点在于上述判断语句。这里就解释到此,时间仓促,希望见谅。
VC的function类说明 -- 继续的更多相关文章
- VC中function函数解析
C++标准库是日常应用中非常重要的库,我们会用到C++标准库的很多组件,C++标准库的作用,不单单是一种可以很方便使用的组件,也是我们学习很多实现技巧的重要宝库.我一直对C++很多组件的实现拥有比较强 ...
- VC++导入导出类
一.导出类 VC++中导出类很简单,下面列出了两个等价的方法: 方法1: class __declspec(dllexport) CTest { public: int m_nValue ...
- VC++中的类的内存分布(上)
0.序 目前正在学习C++中,对于C++的类及其类的实现原理也挺感兴趣.于是打算通过观察类在内存中的分布更好地理解类的实现.因为其实类的分布是由编译器决定的,而本次试验使用的编译器为VS2015 RC ...
- VC++中的类的内存分布(上)(通过强制转换,观察地址,以及地址里的值来判断)
0.序 目前正在学习C++中,对于C++的类及其类的实现原理也挺感兴趣.于是打算通过观察类在内存中的分布更好地理解类的实现.因为其实类的分布是由编译器决定的,而本次试验使用的编译器为VS2015 RC ...
- JavaScript学习总结(十五)——Function类
在JavaScript中,函数其实是对象,每个函数都是Function类的实例,既然函数对象,那么就具有自己的属性和方法,因此,函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定. 一.函数的 ...
- js面向对象设计之function类
本文仅探讨如何合理的使用 function 在 javascript中实现一个面向对象设计的类.总所周知,javascript 并不能实现一个真正意义上的类,比如 protect 比如 函数重载.下面 ...
- JavaScript语言精粹--Function,类,this,对象
1.类与对象 在JS中,创建对象(Create Object)并不完全是我们时常说的创建类对象,JS中的对象强调的是一种复合类型,JS中创建对象及对对象的访问是极其灵活的. JS对象是一种复合类型,它 ...
- 【C++对象模型】使用gcc、clang和VC++显示C++类的内存布局
引言 各种C++实现对C++类/对象的内存布局可能有所不同,包括数据成员的顺序.虚函数表(virtual table: vtbl)的结构.继承关系的处理等.了解C++类/对象的布局,对于理解C++各种 ...
- 钩子函数 Function类
Function 为 com.google.common.base包下接口类: public interface Function<F, T> { @Nullable T apply(@N ...
随机推荐
- 01 node.js,npm,es6入门
Node.js安装 1.下载对应你系统的Node.js版本: https://nodejs.org/en/download/ 命令提示符下输入命令 node -v 会显示当前node的版本 快速入门 ...
- mvc core2.1 Identity.EntityFramework Core 注册 (二)
Startup.cs-> Configure app.UseAuthentication(); //启动验证 Controllers->AccountController.cs 新建 us ...
- (11)线程池(最新的concurrent.futures包去开启)
'''concurrent.futures是最新的开启线程池的包'''import timefrom concurrent.futures import ThreadPoolExecutor #开启线 ...
- C++学习(三十四)(C语言部分)之 链表
1.栈和队列 操作 增查改删重点 插入删除先进先出 -->队列先进后出 -->栈2.链表 写之前先画图存储数据的方式 通过指针将所有的数据链在一起数据结构的目的 管理存储数据 方便快速查找 ...
- vue的理解
vue提供的MVVM框架模式的数据双向绑定,实现了HTML和js的代码分离,提高代码的维护性 vue.js的核心思想包括:数据驱动和组件化思想. 如果没有中间的ViewModel则关系图编程下面所示: ...
- Sencha Touch app example -- oreilly app 分析
from: 2013/8/30的笔记 使用development.js 读取 app.json 配置文件 app.json 配置了app.js文件 app.js lauch function ,首先用 ...
- windows下能搭建php-fpm吗 phpstudy
这个Windows和Linux系统是不一样的,因为一般nginx搭配php需要php-fpm中间件,但是Windows下需要第三方编译. 下载的包里有php-cgi.exe 但不是php-fpm如果想 ...
- Fedora Redhat Centos 有什么区别和关系?
Fedora Redhat Centos 有什么区别和关系? 经常看到有人讨论服务器的操作系统,比如 Readhat 和 Centos,还有 Ubuntu Server. 可能 Ubuntu Serv ...
- vsphere和vmware快照的不足之处
当快照创建时虚拟机执行一个读操作,hypervisor会检查快照VMDK,查看是否有被读取的区块存在.如果有,则从快照中为虚拟机提供这个区块,如果没有,虚拟机还需要去读取基础VMDK.如果只有一个快照 ...
- 设计一个 硬件 实现的 Dictionary(字典)
Dictionary 就是 字典, 是一种可以根据 Key 来 快速 查找 Value 的 数据结构 . 比如 我们在 C# 里用到的 Dictionary<T>, 在 程序设计 里, 字 ...