z****e 发帖数: 2024 | 1 template
void apply(Seq& sq, R(T::*f)(A) const, A a) {
typename Seq::iterator it = sq.begin();
while(it != sq.end())
((*it++)->*f)(a);
}
请问
1. how does the compiler understand T::*f? a member function of T, which
returns R and take A? what does "f" represent?
2. if i change T::*f to be T::*g, all other code remain the same, is this
still alright? |
d****p 发帖数: 685 | 2
Yes
OK if you change *f(a) to *g(a). It just is a parameter name
【在 z****e 的大作中提到】 : template : void apply(Seq& sq, R(T::*f)(A) const, A a) { : typename Seq::iterator it = sq.begin(); : while(it != sq.end()) : ((*it++)->*f)(a); : } : 请问 : 1. how does the compiler understand T::*f? a member function of T, which : returns R and take A? what does "f" represent? : 2. if i change T::*f to be T::*g, all other code remain the same, is this
|
z****e 发帖数: 2024 | 3 这个 *f 根本就没有声明,怎么能直接就用呢?
而且原来的T里边,对应的函数,也不叫f呀。
从来没有见过这种,没有声明过的identifier。
而且如果是T里边的,至少要用typename意思一下吧。
很糊涂。
【在 d****p 的大作中提到】 : : Yes : OK if you change *f(a) to *g(a). It just is a parameter name
|
d****p 发帖数: 685 | 4
Aren't declaring it in the parameter list?
It is a pointer to member function, not a specific member function.
It is not a type; it is a variable.
【在 z****e 的大作中提到】 : 这个 *f 根本就没有声明,怎么能直接就用呢? : 而且原来的T里边,对应的函数,也不叫f呀。 : 从来没有见过这种,没有声明过的identifier。 : 而且如果是T里边的,至少要用typename意思一下吧。 : 很糊涂。
|
z****e 发帖数: 2024 | 5 我貌似明白了许多。
【在 d****p 的大作中提到】 : : Aren't declaring it in the parameter list? : It is a pointer to member function, not a specific member function. : It is not a type; it is a variable.
|
d****p 发帖数: 685 | 6
~~~~ :-)
There are two small issues in the code.
1. Since the member function is const, the iterator should better be const
iterator
2. It is a perfect case to illustrate use of functor. The code you showed
have two performance problems (1).
the seq.end() is called multiple times (2) the f cannot be inlined due to
use of member function pointer. It will
if we use functor to replace the function pointer.
【在 z****e 的大作中提到】 : 我貌似明白了许多。
|