Suppose I have a pointer to data member and I want to know if it's const or not. In other words:
struct S {
const int i; // this is const
int j;
};
In C++ I used to do something like this:
template<typename Class, typename Type, Type Class:: *>
struct is_const_data_member: std::false_type {};
template<typename Class, typename Type, const Type Class:: *Member>
struct is_const_data_member<Class, const Type, Member>: std::true_type {};
template<typename Class, typename Type, Type Class:: *Member>
void foo() {
const auto bar = is_const_data_member<Class, Type, Member>::value;
// ...
}
However, now there is the auto
template parameter and template parameters list are much elegant:
template<auto Member>
void foo() {
// ...
}
In this case, the solo way I found to know if the data member point to something that is const is:
const auto bar = std::is_const_v<std::remove_reference_t<decltype(std::declval<Class>().*Member)>>;
However, it looks ugly to me and I feel like there must be a better way to do it.
Is there any other (shorter) solution for that?
How about something like this:
And then, for example
working test here