Search code examples
c++templatespointer-to-member

Converting a pointer-to-member type to a simple pointer type


I have the following type, which I get with decltype

QString UserInfo::*&

I can remove the & part by wrapping decltype with std::remove_reference_t but I also want to remove UserInfo::* part

How can I do that so I can use only QString type in my templates

I'm using this template in initializer list where I don't have access to solid object or this pointer to .* operator in decltype


Solution

  • Using a valid object is not necessary in unevaluated contexts (like decltype). To exaggerate a little, you could even dereference a null pointer in there, and nothing bad would happen, since the dereference is never actually evaluated.

    To create an object of a type that is not valid, but can be used in unevaluated contexts, you can use std::declval.

    template<class T>
    using member_type = decltype(std::declval<UserInfo>().*std::declval<T>());