无法理解此模板参数
Unable to understand this Template parameter
也许是流感,或者我只是太蠢了,但我无法理解 this Crow 框架代码的一部分。我的内部 C++ 解析器失败。
template <typename MW>
struct check_before_handle_arity_3_const
{
template <typename T,
//this line
void (T::*)(int, typename MW::context&) const = &T::before_handle
>
struct get
{ };
};
我知道这是模板声明中的模板参数。看起来可能是一些 lambda 或函数指针类型参数……但是,我不确定。
有人可以解释这条线吗?
更新:
探索新获得的知识的深度 - 在给出答案后 - 使我从一个伟大的 book:
中摘录
A template can accept a pointer to a function as a nontype template
parameter. (Most often in this book, nontype template parameters are
integral values.) [...] Using a pointer to a function as a nontype
template argument means that we no longer need to store it in the map.
This essential aspect needs thorough understanding. The reason we
don't need to store the pointer to a function is that the compiler has
static knowledge about it. Thus, the compiler can hardcode the
function address in the trampoline code.
所以,现在我知道使用这种技术的原因之一了。
void (T::*)(int, typename MW::context&) const
是非类型 template parameter。
它是指向 T
.
的成员函数的指针
随着= &T::before_handle
的使用,其默认值设置为&T::before_handle
。
我使用这种技术的原因是支持两种处理程序格式:
https://github.com/ipkn/crow/blob/master/include/middleware.h#L17
这里使用check_before_handle_arity_3_const
:
template <typename T>
struct is_before_handle_arity_3_impl
{
template <typename C>
static std::true_type f(typename check_before_handle_arity_3_const<T>::template get<C>*);
template <typename C>
static std::true_type f(typename check_before_handle_arity_3<T>::template get<C>*);
template <typename C>
static std::false_type f(...);
public:
static const bool value = decltype(f<T>(nullptr))::value;
};
对于 SFINAE,is_before_handle_arity_3_impl<T>::value
确定我们是否可以使用 3 个参数调用处理程序。通过使用 std::enable_if
,Crow 可以调用适当的处理程序:
https://github.com/ipkn/crow/blob/master/include/http_connection.h#L110
也许是流感,或者我只是太蠢了,但我无法理解 this Crow 框架代码的一部分。我的内部 C++ 解析器失败。
template <typename MW>
struct check_before_handle_arity_3_const
{
template <typename T,
//this line
void (T::*)(int, typename MW::context&) const = &T::before_handle
>
struct get
{ };
};
我知道这是模板声明中的模板参数。看起来可能是一些 lambda 或函数指针类型参数……但是,我不确定。 有人可以解释这条线吗?
更新: 探索新获得的知识的深度 - 在给出答案后 - 使我从一个伟大的 book:
中摘录A template can accept a pointer to a function as a nontype template parameter. (Most often in this book, nontype template parameters are integral values.) [...] Using a pointer to a function as a nontype template argument means that we no longer need to store it in the map. This essential aspect needs thorough understanding. The reason we don't need to store the pointer to a function is that the compiler has static knowledge about it. Thus, the compiler can hardcode the function address in the trampoline code.
所以,现在我知道使用这种技术的原因之一了。
void (T::*)(int, typename MW::context&) const
是非类型 template parameter。
它是指向 T
.
随着= &T::before_handle
的使用,其默认值设置为&T::before_handle
。
我使用这种技术的原因是支持两种处理程序格式: https://github.com/ipkn/crow/blob/master/include/middleware.h#L17
这里使用check_before_handle_arity_3_const
:
template <typename T>
struct is_before_handle_arity_3_impl
{
template <typename C>
static std::true_type f(typename check_before_handle_arity_3_const<T>::template get<C>*);
template <typename C>
static std::true_type f(typename check_before_handle_arity_3<T>::template get<C>*);
template <typename C>
static std::false_type f(...);
public:
static const bool value = decltype(f<T>(nullptr))::value;
};
对于 SFINAE,is_before_handle_arity_3_impl<T>::value
确定我们是否可以使用 3 个参数调用处理程序。通过使用 std::enable_if
,Crow 可以调用适当的处理程序:
https://github.com/ipkn/crow/blob/master/include/http_connection.h#L110