Template specialization "const T" does not trigger member functions?

Hi,

seems like my compiler (VS2010) does not specialize templates, when the specialization for T is "const T" and I pass a const member function.

Is that normal? Is there another way to write the remove_const template below?

1
2
3
4
5
6
7
8
9
10
11
12
struct S {};

template<typename T> struct remove_const { typedef T type; };
template<typename T> struct remove_const<const T> { typedef T type; };

template<typename A, typename B> struct same_type { enum { value = 0 }; };
template<typename A> struct same_type<A,A> { enum { value = 1 }; };

int main()
{
  static_assert(same_type<remove_const<void(S::*)()const>::type, void(S::*)()>::value == 1, " :-( ");
}
This is a stab in the dark - You are passing a pointer not an actual function - the pointer is non-constant
nope.. I am passing a type.


(btw: there is not even any function in the struct S which I could point to ;). But you're right. The C++ syntax is really tricky on this stuff.. void(S::*foobar)()const would be a variable declaration of a pointer-to-function of type void(S::*)()const where the variable name is foobar. Something like &S::foobar would have been a pointer to a function "foobar" if that function would exist.)


Edit: oh, wait.. you mean I pass a non-const pointer to a const function, not a const pointer to a non-const function... hm... but in this case, T* should specialize, which doesn't do either.

Then again... a pointer to a member function is not a normal pointer anyway. (E.g. you can't assign it to void*.)
Last edited on
In test in the original post fails because the pointers don't point to the same type of functions ( a non-constant member function is not the same as a constant one) - so the two S::* pointers
types are different.


So now:
1
2
3
4
5
6
7
 //OK (but NOT invoking  struct remove_const< const T> template) 
//this corrects the original post
 static_assert(same_type<remove_const<void(S::*)()const>::type, void(S::*)() const>::value == 1, " :-( ");
 
 //OK - This one will invoke the struct remove_const< const T > template
  static_assert(same_type<remove_const<void( S::* const)()const>::type, void(S::*)() const>::value == 1, " :-( ");
 
Topic archived. No new replies allowed.