C++ template type traits for class members I have written type traits for checking, if given class T has_member, then if it either has_non_static_function_member, has_static_function_member or has_non_function_member. These type traits don't take function arguments of the member nor return type to check signature validity (I will provide other type traits for this matter).

I want to know if it can be done somehow simpler, and if the current code doesn't have some logical errors. Personally, I think I might have overcomplicated the implementation.

Here is initial version:

#include &lt;iostream&gt;
#include &lt;type_traits&gt;

template&lt;class, class = void&gt;
struct has_member_xxx : std::false_type
{ };

template&lt;class T&gt;
struct has_member_xxx&lt;
    T,
    std::void_t&lt;
        decltype(&amp;T::xxx)
        &gt;
    &gt; : std::true_type
{ };

template&lt;class, class = void&gt;
struct has_non_static_member_function_xxx : std::false_type
{ };

template&lt;class T&gt;
struct has_non_static_member_function_xxx&lt;
    T,
    std::enable_if_t&lt;
        std::is_member_function_pointer&lt;
            decltype(&amp;T::xxx)
        &gt;::value&gt;
    &gt; : std::true_type
{ };

template&lt;class, class = void&gt;
struct has_static_member_function_xxx : std::false_type
{ };

template&lt;class T&gt;
struct has_static_member_function_xxx&lt;
    T,
    std::enable_if_t&lt;
        (!has_non_static_member_function_xxx&lt;T&gt;::value) &amp;&amp;
        std::is_function&lt;typename std::remove_pointer&lt;decltype(&amp;T::xxx)&gt;::type&gt;::value
        &gt;
    &gt; : std::true_type
{ };

template&lt;class, class = void&gt;
struct has_non_function_member_xxx : std::false_type
{ };

template&lt;class T&gt;
struct has_non_function_member_xxx&lt;
    T,
    std::enable_if_t&lt;
        has_member_xxx&lt;T&gt;::value &amp;&amp;
        (!has_non_static_member_function_xxx&lt;T&gt;::value) &amp;&amp;
        (!has_static_member_function_xxx&lt;T&gt;::value)
        &gt;
    &gt; : std::true_type
{ };

struct A
{
    int xxx;
};

struct B 
{
    int xxx();
};

struct C 
{
    static int xxx();
};

int main()
{ 
    std::cout &lt;&lt; std::boolalpha;
    std::cout &lt;&lt; "has member xxx: " &lt;&lt; std::endl;
    std::cout &lt;&lt; "A: " &lt;&lt; has_member_xxx&lt;A&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "B: " &lt;&lt; has_member_xxx&lt;B&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "C: " &lt;&lt; has_member_xxx&lt;C&gt;::value &lt;&lt; std::endl &lt;&lt; std::endl;

    std::cout &lt;&lt; "has non-static member function xxx" &lt;&lt; std::endl;
    std::cout &lt;&lt; "A: " &lt;&lt; has_non_static_member_function_xxx&lt;A&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "B: " &lt;&lt; has_non_static_member_function_xxx&lt;B&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "C: " &lt;&lt; has_non_static_member_function_xxx&lt;C&gt;::value &lt;&lt; std::endl &lt;&lt; std::endl;

    std::cout &lt;&lt; "has static member function xxx" &lt;&lt; std::endl;
    std::cout &lt;&lt; "A: " &lt;&lt; has_static_member_function_xxx&lt;A&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "B: " &lt;&lt; has_static_member_function_xxx&lt;B&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "C: " &lt;&lt; has_static_member_function_xxx&lt;C&gt;::value &lt;&lt; std::endl &lt;&lt; std::endl;

    std::cout &lt;&lt; "has non-function member xxx" &lt;&lt; std::endl;
    std::cout &lt;&lt; "A: " &lt;&lt; has_non_function_member_xxx&lt;A&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "B: " &lt;&lt; has_non_function_member_xxx&lt;B&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "C: " &lt;&lt; has_non_function_member_xxx&lt;C&gt;::value &lt;&lt; std::endl &lt;&lt; std::endl;
}


Code is also on Coliru for convenience.
If xxx is a non-accessible data member (e.g. private) your code fails to compile with gcc. However this seems to be gcc bug. (stackoverflow question) Still if your code is supposed to be compilable with gcc you should consider this.
The templates give false for non-accessible (e.g. private) members. That is usually good though, because someone using quering about a class should only see the public interfacce anyway. Still consider what the intended behavior of your templates should be.
If xxx is of reference type, then your has_member_xxx will give false, because taking an address to member of reference type is not allowed.
has_member_xxx is also false if xxx is a static data member with deleted or non-accessible operator&amp;(), although this is a pathological case because I don't see a good reason to use operator&amp;() in that way. You might want to take std::addressof instead to avoid such a case.
has_member_xxx will also not detect member function templates, because the address of a function template cannot be taken. However that is probably expected behavior.
The same issue also arises for overloaded member functions, though. The adress of overloaded function can also not be taken. This is probably not intended behavior.


If you could specify exactly what your templates are supposed to detect / not detect that would be helpful.