int(int, int) style template function type syntax

老子叫甜甜 提交于 2020-01-02 03:36:08

问题


I remember that when using Boost.Spirit and for the std::function addition to C++0x, you specify the function type by using a syntax that doesn't use pointers, like in defining std::function<bool(int)> fn, whereas you would cast a pointer like (bool(*)(int))fn.

Can anyone tell me the name of this new syntax or any references on this, or how to use it? It seems like a polymorphic function type syntax that applies for functors as well, but I don't really know how to use it.


回答1:


bool(int) is the type of the function; bool(*)(int) is the type of the function pointer. In other words, if you define

typedef bool(BF)(int);
typedef bool(pBF*)(int);

then BF* is the same as pBF.

The std::function template captures the return and argument types via (variadic) templates:

template <typename R, typename ...Args> struct function
{
  function(R(&f)(Args...)); // conceptually
}



回答2:


It is not new sintax, although old compilers did sometimes reject it. It is simply the type of a function versus the type of a function pointer, similar to the type of an arrays versus the pointer to an array.

The facts that there are no function l-values and that function r-values decay quickly to pointer-to-functions make them mostly useless. Except in the case of templates, of course.



来源:https://stackoverflow.com/questions/7027922/intint-int-style-template-function-type-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!