How to determine whether a class has a function with a specified function name at compile time ?
#define HAS_MEMBER(member)\ template<typename T, typename... Args>struct
has_member_##member\ {\ private:\ template<typename U> static auto Check(int) ->
decltype(std::declval<U>().member(std::declval<Args>()...), std::true_type()); \
template<typename U> static auto Check(...) -> decltype(std::false_type()); \
public:\ static const bool value = std::is_same<decltype(Check<T>(0)), std
::true_type>::value; \ }; \

We used this `decltype` keyword , This keyword has the ability to push type , But it does not execute its expression , So it can do a lot of things at compile time , Here's how to use it and the compiler will do its best to make your code the right feature to do this .
Look at the first sentence :
template<typename U> static auto Check(int) ->
decltype(std::declval<U>().member(std::declval<Args>()...), std::true_type());
We just want to see if an expression like this exists in a class Class.member(...)
. So here it is , stay decltype Internal comma operation expression ,, If there is such a form , Then you can match the first of the comma expressions
std::declval<U>().member(std::declval<Args>()...)
, It shows that there is such a function , Because the comma expression returns the value to the right of the last comma , actual decltype The type of derivation is std::true_type type , Should be decltype Only root expression , So we instantiate one std::true_type Object of , Use its constructor as an expression , If there is a representation of the specified function name , this Check(int) The return value type of the function is std::true_type.
So if it's a comma expression , What if the first expression doesn't match , It just doesn't exist , So how do we show that ? We're going to use the compiler, which will want you to do it through the characteristics of compilation .
template<typename U> static std::false_type Check(...);

Be on top Check When function matching cannot be satisfied , Then there is no function with the specified function name , It will match this Check function , Then its return value type is std::false_type, So we can deduce from the test Check Returns a value type to determine whether the function containing the specified function name is included .
static const bool value = std::is_same<decltype(Check<T>(0)), std::true_type>:
:value; HAS_MEMBER(a) HAS_MEMBER(c) struct A { A(int) {} void b() {} void c(int,
bool) {} }; int main() { std::cout << has_member_a<A>::value << std::endl; std::
cout << has_member_c<A, int, bool>::value << std::endl; return 0; }

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network