Your approach is fundamentally flawed, because decltype(T::x)
is perfectly valid for non-static
members:
#include <iostream>
struct T
{
int x;
};
int main()
{
std::cout << sizeof(decltype(T::x)) << '\n';
}
// Output: 4
You can get what you want with std::is_member_pointer
:
If
T
is pointer to non-static member object or a pointer to non-static member function, provides the member constant value equaltrue
. For any other type, value isfalse
.
#include <iostream>
#include <type_traits>
struct T
{
int x;
};
struct S
{
static int x;
};
int main()
{
std::cout << !std::is_member_pointer<decltype(&T::x)>::value << ' ';
std::cout << !std::is_member_pointer<decltype(&S::x)>::value << '\n';
}
// Output: 0 1
This works because access to members through the ::
syntax results in a pointer-to-member only if the member is not-static
; it is of course a normal variable access when the member is static
.