简体   繁体   English

除了指向数据成员的指针之外,如何只接受模板中的可调用对象?

[英]How to accept only callable objects in templates except pointer to data members?

I want to create a templated function which accepts and invokes a callable object(except pointer to data members) with arguments to pass to it.我想创建一个模板化的 function,它接受并调用一个可调用对象(指向数据成员的指针除外),并将 arguments 传递给它。 I want the template to only accept the following types:-我希望模板只接受以下类型:-

  1. Pointers to functions函数指针
  2. Pointers to member functions指向成员函数的指针
  3. Lambda Lambda
  4. bind expressions绑定表达式
  5. std::function标准::function
  6. Functors函子

Like this...像这样...

template< class Function, class... Args >
explicit X( Function&& f, Args&&... args );

But the first argument is accepting any type and I want to create some validation such that it only accept callable objects and throw error(preferably in compile-time) if it invalidates.但是第一个参数是接受任何类型,我想创建一些验证,以便它只接受可调用对象并在它无效时抛出错误(最好在编译时)。

There's a C++20 concept just for this purpose:有一个 C++20 概念就是为了这个目的:

template<class Function, class... Args>
    requires std::invocable<Function, Args...>
void X(Function&& f, Args&&... args);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM