是一个 is_functor C++特质类别可能吗?
如果参数是 C++ 函数对象(函子),如何静态推断?
template <typename F>
void test(F f) {}
我尝试了 is_function
,但这不起作用。似乎也没有 is_functor
特征,所以也许这是不可能的。我似乎只是在寻找特定的成员函数,在本例中是函数调用运算符:F::operator()
。
How can I deduce statically if an argument is a C++ function object (functor)?
template <typename F>
void test(F f) {}
I tried is_function<F>::value
, but this doesn't work. It also seems there is no is_functor
trait, so perhaps it's not possible. I appear to be only looking for a specific member function, in this case the function call operator: F::operator()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
创建这样的特征是可能的,但有两个限制:
operator()
的类函子有着本质上的不同。因此我们在实施时必须分别对待这两种情况。但这对于使用来说不是问题,我们可以向用户隐藏这个实现细节。第一步:自由函数
让我们从自由函数开始,因为它们不太容易检测。我们的任务是,当给定函数指针时,确定该函数指针的签名是否与作为第二个模板参数传递的签名匹配。为了能够比较它们,我们要么需要掌握底层函数签名,要么创建我们签名的函数指针。我任意选择了后者:
现在剩下要做的就是比较,我们已经完成了自由函数部分:
第二步:类函子
这一步涉及的更多一些。我们可以使用 SFINAE 轻松检测类是否定义了operator():
但这并不能告诉我们是否存在我们所需的函数签名!幸运的是,我们可以在这里使用一个技巧:指针是有效的模板参数。因此,我们可以首先使用所需签名的成员函数指针,并检查
&T::operator()
是否属于该类型:现在
check
仅当C
确实具有void C::operator()() 时才是有效的模板实例化常量。但要做到这一点,我们首先必须将
C
和成员函数指针的签名结合起来。正如我们已经看到的,我们需要担心两种我们不必关心自由函数的额外情况:const 和 volatile 函数。除此之外,它几乎是相同的:将其与我们关于
check
辅助结构的发现放在一起,我们得到函子对象的 check 元函数:第三步:将各个部分放在一起
我们快完成了。现在我们只需要决定何时使用我们的自由函数,以及何时使用类函子元函数。幸运的是,C++11 为我们提供了一个可以用于此目的的
std::is_class
特征。所以我们所要做的就是专门研究一个布尔参数:所以我们终于可以添加最后一块拼图,成为我们实际的
is_callable
特征:现在我们清理我们的代码,将实现细节放入匿名中命名空间,因此它们无法在我们的文件外部访问,并且有一个很好的
is_callable.hpp
可以在我们的项目中使用。完整代码
带有一些测试的 Ideone 示例
http://ideone.com/ 7PWdiv
It is possible to create such a trait, with two restrictions:
operator()
. Thus we have to treat both cases seperately when implementing. This is not a problem for usage though, we can hide this implementation detail from the user.Step one: Free functions
Let's start with free functions, because they are little easier to detect. Our task is, when given a function pointer, to determine whether the signature of that function pointer matches the signature passed as the second template argument. To be able to compare those, we either need to get a grasp of the underlying function signature, or create a function pointer of our signature. I arbitrarily chose the latter:
Now all that's left to do is to compare and we are done with the free function part:
Step two: Class functors
This one is a little more involved. We could easily detect with SFINAE whether a class defines an
operator()
:but that does not tell us whether one exists for our desired function signature! Luckily, we can use a trick here: pointers are valid template parameters. Thus we can first use the member function pointer of our desired signature, and check whether
&T::operator()
is of that type:Now
check<void (C::*)() const, &C::operator()>
will only be a valid template instantiation ifC
does indeed have avoid C::operator()() const
. But to do this we first have to combineC
and the signature to a member function pointer. As we already have seen, we need to worry about two extra cases we did not have to care about for free functions:const
andvolatile
functions. Besides that it's pretty much the same:Putting that and our findings concerning the
check
helper struct together, we get our check metafunction for functor objects:Step three: Putting the pieces together
We are almost done. Now we only need to decide when to use our free function, and when the class functor metafunctions. Luckily, C++11 provides us with a
std::is_class
trait that we can use for this. So all we have to do is specialize on a boolean parameter:So we can finally add the last piece of the puzzle, being our actual
is_callable
trait:Now we clean up our code, put implementation details into anonymous namespaces so they are not acessible outside of our file, and have a nice
is_callable.hpp
to use in our project.Full code
Ideone example with some tests
http://ideone.com/7PWdiv
虽然这不适用于重载函数,但对于所有其他情况(自由函数、实现
operator()
的类和 lambda),这个简短的解决方案适用于 C++11:注意:
std::is_invocable
自 C++17 起可用。Although this does not work for overloaded functions, for all other cases (free functions, classes implementing
operator()
, and lambdas) this short solutions works in C++11:Note:
std::is_invocable
is available since C++17.您可以在c++20中使用以下概念
You can use the following concept in c++20
是的,可以手动实现函子的验证。
示例:
也就是说,用于验证类是否具有函数调用重载运算符(函子)的逻辑与上面的代码类似。
也就是说,如果
std::function
接受类的实例Test{}
则表示该类有函子,否则没有t。可能的解决方案示例
这是源代码:
示例用法:
is_functor
->真实结果is_functor
-> False 结果有关 std::is_constructible 的信息
是一个特征类,用于标识 T 是否是具有 Arg 指定的参数类型集的可构造类型。
对于此类,可构造类型是可以使用一组特定参数构造的类型。
is_constructible
继承自integral_constant
,为true_type
或false_type
,具体取决于 T 是否可使用参数列表构造参数。简而言之,它检查给定的类是否有构造函数,例如:
std::is_constructible
-> ;真实结果std::is_constructible
->错误结果实现示例:
最终解释
在
is_functor
的实现中,检查了std::function
是否接受Test 的实例{}
,这是真的。参考文献:
std::is_constructible是如何实现的?实现了吗?
能否在 C++11 中模拟 std::is_invocable ?
https://replit.com/@LUCASP6/RowdyAlphanumericCode#main.cpp
Yes, it is possible to manually implement a validation for functors.
Example:
That said, the logic that will be used to validate if a class has a function call overload operator (functor) is similar to the code above.
That is, if the
std::function<void(int)>
accepts an instance of the classTest{}
means the class has a functor, otherwise it doesn't.Example of an possible solution
Here is the source code:
Example usage:
is_functor<Test, int>
-> True resultis_functor
-> False resultInfo about std::is_constructible
Is a trait class that identifies whether T is a constructible type with the set of argument types specified by Arg.
For this class, a constructible type is a type that can be constructed using a particular set of arguments.
is_constructible
inherits fromintegral_constant
as being eithertrue_type
orfalse_type
, depending on whether T is constructible with the list of arguments Args.In short, it checks if a given class has a constructor, for example:
std::is_constructible<Test2, bool, int>
-> True resultstd::is_constructible<Test2, float>
-> False resultExample of implementation:
Final explanation
In the implementation of
is_functor
it was checked ifstd::function<void(int)>
accepts an instance ofTest{}
, which is true.References:
How is std::is_constructible<T, Args> implemented?
Can std::is_invocable be emulated within C++11?
https://replit.com/@LUCASP6/RowdyAlphanumericCode#main.cpp
修改自此答案。
它可以像...
Altered from this answer.
It could be used like...