I get a real kick from exploring unusual C ++ angles. Having learned about the real types of functions, and not about function pointers from this question , I tried to mess around with the typing function and came up with this strange case:
typedef int Func(int); int Foo(int x) { return 1; } int main() { const Func*& f = &Foo; return 0; }
Since &Foo is an rvalue of type Func* , I decided that I should be able to put it in a const link, but I get this error from g ++ 4.6:
funcTypes.cpp: In function 'int main()': funcTypes.cpp:7:23: error: invalid initialization of non-const reference of type 'int (*&)(int)' from an rvalue of type 'int (*)(int)'
But f is const! It became obvious to me that the application of const to a function (or link / link to a pointer, etc.) is simply ignored; this code compiles just fine:
template <typename A, typename B> struct SameType; template <typename A> struct SameType<A, A> { }; typedef int Func(int); int main() { SameType<const Func, Func>(); return 0; }
I suppose this speeds up the removal of their is_function , but my question is: why does C ++ allow this by ignoring it and not banning it?
EDIT: Now I understand that in the first example, f not const and that const FuncPtr& f = &Foo works. However, it was just the background, the real question is the one above.
source share