How to remove const from a signature of a member function template type?

I am working on some system files like C ++, and I am having the problem of removing a constant from a member function for use with function classes. What really bothers here is that it works fine with g ++, but MSVC10 does not handle partial specialization correctly, and I don’t know if one of these compilers really has an error.

The question is, what is the correct way to remove the constant determinant from a member function so that I can get the signature of the function type?

Take the following code example:

#include <iostream> template<typename T> struct RemovePointer { typedef T Type; }; template<typename T> struct RemovePointer<T*> { typedef T Type; }; template<typename R,typename T> struct RemovePointer<R (T::*)> { typedef R Type; }; class A { public: static int StaticMember() { return 0; } int Member() { return 0; } int ConstMember() const { return 0; } }; template<typename T> void PrintType(T arg) { std::cout << typeid(typename RemovePointer<T>::Type).name() << std::endl; } int main() { PrintType(&A::StaticMember); PrintType(&A::Member); PrintType(&A::ConstMember); // WTF? } 

All three of these PrintType statements must print the same thing. MSVC10 prints the following:

 int __cdecl(void) int __cdecl(void) int (__cdecl A::*)(void)const __ptr64 

g ++ prints this (which is the expected result):

 FivE FivE FivE 
+4
source share
3 answers

I suggest you take a look at TypeTraits.h of the loki library from Alexandrescu. It provides a general way to remove qualifiers such as const.

http://loki-lib.cvs.sourceforge.net/loki-lib/loki/include/loki/TypeTraits.h?view=markup

When I have some philosophical problems with metaprogramming c ++ metaprograms, I tend to look in Modern C ++ design if there is an answer to my whereabouts.

+3
source

This would help:

 template<typename R,typename T> struct RemovePointer<R (T::*)() const> { typedef R Type; }; 

Note that you probably want to add () in the previous line as well (otherwise it will correspond to both pointers to elements and pointers to functions):

 template<typename R,typename T> struct RemovePointer<R (T::*)()> { typedef R Type; }; 
+1
source

typeid(...).name() returns the string defined by the implementation. It can be a compiler-distorted character or a poem written by John Skeet. Please do not rely on this to do anything useful.

It also seems strange to want to take away "const"; const function, so why don't you want this in the resulting string?

I have no idea why you expect or see "FIvE". I do not see anything like this in your code.

+1
source

Source: https://habr.com/ru/post/1345243/


All Articles