Re: Member function pointers to member functions with default arguments
* Hamish -> Alf P. Steinbach:
#include <iostream>
using namespace std;
class X {
public:
void f(int i = 0) { cout << "i = " << i << endl; }
};
template< typename Fn >
void func(Fn f, X &x) {
(x.*f)();
}
int main() {
X x;
func(&X::f, x);
return 0;
}
// Prints: i = 0.
I was a bit surprised it worked, to be honest. As you said, &X::f
has type void (X::*)(int) so I would have thought Fn would resolve
as void (X::*)(int) causing a compile error on the line (x.*f)();.
Oh well, I'm not complaining.
You should be. :-)
<comeau>
Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 11: error: too few arguments in function call
(x.*f)();
^
detected during instantiation of
"void func(Fn, X &) [with Fn=void (X::*)(int)]" at
line 16
1 error detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
Hit the Back Button to review your code and compile options.
Compiled with C++0x extensions enabled.
</comeau>
<msvc>
vc_project.cpp(11) : error C2198: 'void (__thiscall X::* )(int)' : too
few arguments for call through pointer-to-functio
n
vc_project.cpp(16) : see reference to function template
instantiation 'void func<void(__thiscall X::* )(int)>(Fn
,X &)' being compiled
with
[
Fn=void (__thiscall X::* )(int)
]
</msvc>
<g++>
<url: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=4205>
</g++>
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?