The following program compiles with both gcc and clang, but is this actually standard C++11 or do both compilers choose to support it for convenience?
struct
Yes, that is standard. [func.wrap.func.inv] specifies that the operator()(ArgTypes&&... args)
of std::function calls
INVOKE
(f, std::forward(20.8.2), where(args)..., R) fis the target object (20.8.1) of*this.
(Where R is the specified return type.)
[func.require] defines INVOKE:
Define
INVOKE(f, t1, t2, ..., tN)as follows:
(t1.*f)(t2, ..., tN)whenfis a pointer to a member function of a classTandt1is an object of typeTor a reference to an object of typeTor a reference to an object of a type derived fromT;
((*t1).*f)(t2, ..., tN)whenfis a pointer to a member function of a classTandt1is not one of the types described in the previous item;- […]
Note that the trailing R in the call is used for the conversion to R (the return type of the function):
Define
INVOKE(f, t1, t2, ..., tN, R)asINVOKE(f, t1, t2, ..., tN)implicitly converted toR.
The first and only argument you give is the pointer to the Foo-object. The call to method thus results in the call (void)((*t1).*f)() which is, when written with your given values, equivalent to
((*(&myFoo)).&Foo::bar)(), which is equivalent to myFoo.bar().