http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52917
Bug #: 52917 Summary: explicitly stated return type in std::mem_fn cannot be compiled Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: freunddeslic...@web.de #include <functional> struct X { int a; int& easy() {return a;} int& get() {return a;} const int& get() const {return a;} }; int main(void) { auto x = std::mem_fn (&X::easy); // (1) works auto y = std::mem_fn <int&>(&X::get ); // (2) ERROR!! auto z = std::mem_fun<int&>(&X::get ); // (3) deprecated in c++11, but works: return 0; } ################################################################################ (2) needs the return type to diffrentiate between the const and the non-const getter. The specification for mem_fn is template< class R, class T > /*unspecified*/ mem_fn(R T::* pm); template< class R, class T, class... Args > /*unspecified*/ mem_fn(R (T::* pm)(Args...)); [ ... some more overloads for const, volatile and references] (2) should result in an instantiation of the second version with sizeof...(Args)==0, because argument to the first version is a "pointer to member data", not "pointer to member function". Both g++-4.7 and clang++-3.1 (when compiling with libstdc++) try to instantiate the first version by mistake. This definitely a library error, because clang when compiling with libc++ correctly instantiates the second version of mem_fn. ################################################################################## g++-4.7 -std=c++0x bug.cpp bug.cpp: In function ‘int main()’: bug.cpp:17:39: error: no matching function for call to ‘mem_fn(<unresolved overloaded function type>)’ bug.cpp:17:39: note: candidate is: In file included from bug.cpp:3:0: /usr/include/c++/4.7/functional:821:5: note: template<class _Tp, class _Class> std::_Mem_fn<_Tp _Class::*> std::mem_fn(_Tp _Class::*) /usr/include/c++/4.7/functional:821:5: note: template argument deduction/substitution failed: /usr/include/c++/4.7/functional: In substitution of ‘template<class _Tp, class _Class> std::_Mem_fn<_Tp _Class::*> std::mem_fn(_Tp _Class::*) [with _Tp = int&; _Class = <missing>]’: bug.cpp:17:39: required from here /usr/include/c++/4.7/functional:821:5: error: creating pointer to member reference type ‘int&’ bug.cpp:17:39: error: unable to deduce ‘auto’ from ‘<expression error>’ #################################################################################### clang++ -std=c++0x bug.cpp bug.cpp:17:12: error: no matching function for call to 'mem_fn' auto y = std::mem_fn <int&>(&X::get ); // (2) ERROR!! ^~~~~~~~~~~~~~~~~~ /usr/lib/gcc/i686-linux-gnu/4.7/../../../../include/c++/4.7/functional:820:5: note: candidate template ignored: substitution failure [with _Tp = int &] mem_fn(_Tp _Class::* __pm) ^ 1 error generated.