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,
Final review of database : Summary of comprehensive application questions use Python Make simple games Laplance operator ( Second derivative ) Convert hard disk to GPT Partition format Python Implementation of Hanoi Tower code about String How to create objects vue3 Learning journey 1—— establish vue3 project java String from back to front _Java String String summary use Python Write a story about plants versus zombies 【 Greedy Algorithm 】 Huffman coding problem