I would like to extract the type of the member a member pointer points to.
template<someType myClass::*member>
void demo(myClass& instance, void* ptr) {
instance.*member = *reinterpret_cast<someType*>(ptr); // can the someType in this line be deduced from member?
}
I tried using decltype
as suggested in the comments, however I have issues with this:
instance.*member= static_cast<decltype(instance.*member)>((*buffer)[offset]);
buffer
is a std::shared_ptr<std::vector<uint8_t>>
,
someType
is uint32_t
I get the following error message:
error: invalid static_cast from type ‘__gnu_cxx::__alloc_traits >::value_type {aka unsigned char}’ to type ‘uint32_t& {aka unsigned int&}’
As far as I understand decltype(instance.*member)
with member
defined as uint32_t instance::*member
yields a reference uint32_t&
rather than uint32_t
. I tried to pass instance by value and the error remains. I am aware of std::remove_reference
however, I do not understand how the reference gets to be there in the first place.
A further improvement would be if I could extract the someType
without a class instance. However I have no clue how to achieve this, while I can get the class without a pointer by having the std lib like:
template <T*>
struct removePointer {
typedef T type;
}
I have no Idea how to write this in a form where I can get the someType
part of the class, without knowing the class first. I could write something like the following however I would still have to pass the class naem and typename explicitly, is there a way to extract these automatically? Furthermore the following doe not compile in the first place (http://ideone.com/8VlKO4):
#include
using namespace std;
template <class C,typename T, T C::*v>
struct getPointerType {
typedef T type;
};
class Test {
int value;
};
int main() {
int Test::*member=nullptr;
cout << typeid(getPointerType<Test, int, decltype(member)>::type) << std::endl;
return 0;
}