I found several questions & answers on SO dealing with detecting at compile time (via SFINAE) whether a given class has a member of certain name, type, or signature. However, I couldn't find one that also applies to static public member functions (when pointer-to-member tricks won't work). Any ideas?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Here's one way:
It's written for a function called
fun
. Use it likehas_fun<T, int(int, int)>::value
.Here's another:
It might be sensible to test whether the return type of the function is convertible to
Ret
instead of checking for exatch match. Useis_convertible
instead ofis_same
in that case and, at the same time, check that return type is different thanNo
(as Yakk points out, there are types out there that can be constructed from just about anything).Following may help: (https://ideone.com/nDlFUE)
Then define a traits:
Simply invoke the member function and discard the result in a SFINAE context. If it succeeds, the method exists. If it fails, the method does not.
use:
which checks if
int r = T::foo( 7 )
is a valid expression, and not for exact signature match.