简体   繁体   中英

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. I want the template to only accept the following types:-

  1. Pointers to functions
  2. Pointers to member functions
  3. Lambda
  4. bind expressions
  5. std::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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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