g*********s 发帖数: 1782 | 1 Function object重载()是C++的规定吗?为何选择()?手头没有D&R,这里有什么故事么?
struct myclass {
bool operator() (int i,int j) { return (i
};
// using object as comp
sort (myvector.begin(), myvector.end(), myclass()); | t****t 发帖数: 6806 | 2 since the name is "function object" (actually, "functor"), you probably
could guess it is a generalized *function*. what is operator() called?
function call operator. isn't it a natural choice?
事么?
【在 g*********s 的大作中提到】 : Function object重载()是C++的规定吗?为何选择()?手头没有D&R,这里有什么故事么? : struct myclass { : bool operator() (int i,int j) { return (i: }; : // using object as comp : sort (myvector.begin(), myvector.end(), myclass());
| g*********s 发帖数: 1782 | 3 有道理。
在C++作者的FAQ上看到下述function object示例。下面这行该怎么理解呢?是重载了
int类型转换操作符吗?
operator int() const { return val; }
class Sum {
int val;
public:
Sum(int i) :val(i) { }
operator int() const { return val; }
int operator()(int i) { return val+=i; }
};
void f(vector v)
{
Sum s = 0; // initial value 0
s = for_each(v.begin(), v.end(), s);
cout << "the sum is " << s << "\n";
// or even:
cout << "the sum is " << for_each(v.begin(), v.end(),
Sum(0)) << "\n";
}
probably
【在 t****t 的大作中提到】 : since the name is "function object" (actually, "functor"), you probably : could guess it is a generalized *function*. what is operator() called? : function call operator. isn't it a natural choice? : : 事么?
| g*********s 发帖数: 1782 | 4 operator int() const { return val; }
这个函数的返回类型是int,参数列表是空。那是否可以更直观地写成:
int operator()(void) const {return val;}
试了一下,编译过不去。是不是对这个signature理解有误?
【在 g*********s 的大作中提到】 : 有道理。 : 在C++作者的FAQ上看到下述function object示例。下面这行该怎么理解呢?是重载了 : int类型转换操作符吗? : operator int() const { return val; } : class Sum { : int val; : public: : Sum(int i) :val(i) { } : operator int() const { return val; } : int operator()(int i) { return val+=i; }
| e****d 发帖数: 895 | 5 It's user defined type conversion operator. You should not put
anything inside the parenthesis.
【在 g*********s 的大作中提到】 : operator int() const { return val; } : 这个函数的返回类型是int,参数列表是空。那是否可以更直观地写成: : int operator()(void) const {return val;} : 试了一下,编译过不去。是不是对这个signature理解有误?
| g*********s 发帖数: 1782 | 6 i see. now i think i understand the code.
the int() operator is defined such that in "cout << s", s is implicitly
converted to an int to output.
s = for_each(v.begin(), v.end(), s) is an assignment that calls the
default assignment operator. we can actually just keep the RHS.
one remaining doubt: what if the class Sum has another float() type
converter defined? in that case which one would cout pick up?
【在 e****d 的大作中提到】 : It's user defined type conversion operator. You should not put : anything inside the parenthesis.
| e****d 发帖数: 895 | 7 Usually, you should overload operator << for cout or any ostream.
In this case, if operator int conflicts with operator float, there
will be ambigious error.
【在 g*********s 的大作中提到】 : i see. now i think i understand the code. : the int() operator is defined such that in "cout << s", s is implicitly : converted to an int to output. : s = for_each(v.begin(), v.end(), s) is an assignment that calls the : default assignment operator. we can actually just keep the RHS. : one remaining doubt: what if the class Sum has another float() type : converter defined? in that case which one would cout pick up?
| g*********s 发帖数: 1782 | 8 sure. overloading << is more natural.
on the other hand, after overloading int(), how does << locate it
properly?
it seems << is very smart. the c++ compiler is defined in such way?
【在 e****d 的大作中提到】 : Usually, you should overload operator << for cout or any ostream. : In this case, if operator int conflicts with operator float, there : will be ambigious error.
| t****t 发帖数: 6806 | 9
no, if you just keep RHS, then the result is lost.
std::for_each send in the functor by value (and return by value, of course).
so the accumulated value is kept in a local copy of functor inside for_each.
this functor is returned and copied again. just like a=a+1, you can write
a+1 (that's a valid expression), but the result is lost.
【在 g*********s 的大作中提到】 : i see. now i think i understand the code. : the int() operator is defined such that in "cout << s", s is implicitly : converted to an int to output. : s = for_each(v.begin(), v.end(), s) is an assignment that calls the : default assignment operator. we can actually just keep the RHS. : one remaining doubt: what if the class Sum has another float() type : converter defined? in that case which one would cout pick up?
| g*********s 发帖数: 1782 | 10 How about this?
Sum s;
for_each(v.begin(), v.end(), s);
The result still gets lost, as for_each is not pass-by-ref?
course).
for_each.
write
【在 t****t 的大作中提到】 : : no, if you just keep RHS, then the result is lost. : std::for_each send in the functor by value (and return by value, of course). : so the accumulated value is kept in a local copy of functor inside for_each. : this functor is returned and copied again. just like a=a+1, you can write : a+1 (that's a valid expression), but the result is lost.
| b******n 发帖数: 592 | 11 I think so. You can get around this by using static member or
giving access to data outside class. normally for_each is not
used for things like this anyway. you would use accumulate.
【在 g*********s 的大作中提到】 : How about this? : Sum s; : for_each(v.begin(), v.end(), s); : The result still gets lost, as for_each is not pass-by-ref? : : course). : for_each. : write
| e****d 发帖数: 895 | 12 You can do something like,
for_each(v.begin(), v.end(), boost::ref(s));
【在 g*********s 的大作中提到】 : How about this? : Sum s; : for_each(v.begin(), v.end(), s); : The result still gets lost, as for_each is not pass-by-ref? : : course). : for_each. : write
|
|