由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - stl Compare为何需要重载()?
相关主题
C++糟粕和需要避免的。c++ 宏的问题
C++ operator = overloading用copy & swap有啥优点C++:如何计算一个类实例化了多少次?
请问在class member function中如何调用overloaded function caC++ 什么时候用 "new" ?
再一个问题c++问一个C++ String的初始化问题
今天给c++震惊了C++怎样设置全局变量
const int foo()啥意思?一个Quant Developer的C++面试题
请教这个程序里用到了什么constructor啊?有几个copy constructor?问个copy constructor的问题
前几天有人问rvalue reference的怎么隐藏c++template代码?只提供lib 不提供头文件
相关话题的讨论汇总
话题: operator话题: int话题: sum话题: each话题: val
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
怎么隐藏c++template代码?只提供lib 不提供头文件今天给c++震惊了
请教C++11的rvalue refconst int foo()啥意思?
warning: returning address of local variable or temporary请教这个程序里用到了什么constructor啊?有几个copy constructor?
[菜鸟问题]类模板问题前几天有人问rvalue reference的
C++糟粕和需要避免的。c++ 宏的问题
C++ operator = overloading用copy & swap有啥优点C++:如何计算一个类实例化了多少次?
请问在class member function中如何调用overloaded function caC++ 什么时候用 "new" ?
再一个问题c++问一个C++ String的初始化问题
相关话题的讨论汇总
话题: operator话题: int话题: sum话题: each话题: val