简体   繁体   中英

Is there a final, simple answer of how to detect if a function exists?

This site has 50+ post about the topic, several 'detection patterns' have had their successes through various epochs. With C++20 we should see quite some simplifications here.

So, what are the current best practices of detecting

  • A. Global functions
  • B. Member functions
  1. An exact signature. Eg detect that F( int ) is defined rather than any F that takes an int via (an implicit) conversion.
  2. Callable with a given type (under the usual rules of implicit conversion)

Basically requires expressions:

// 1
bool exists = requires(Sig &sig) { sig = thefunction; }
// 2
bool exists = requires(A a) { thefunction(a); }
// 2B
bool exists = requires(C &c, A a) { c.thefunction(a); }

Sig should be:

using Sig = RV (*) (Args); // A
using Sig = RV (Class::*) (Args); // B

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