由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问个函数指针指向操作符的问题
相关主题
面试问题C++命名空间和算子重载
问个c++的弱问题请问c++中操作符可以声明为虚函数吗?
C语言里面的register变量能否进行取地址操作? (转载)member and friend
0 < -1 ? A c++ question能否对某个库进行操作符重载?
问个c++的template的问题有个SB interviewer和我说++i比i++好 (转载)
string operator +基本功不扎实,问个问题
重载 ^ 操作符编译出错请问关于overloading <<
overriding operator<<*(&b1)=b编译不过,b1=b可以,区别是?
相关话题的讨论汇总
话题: operator话题: int话题: op话题: function话题: defined
进入Programming版参与讨论
1 (共1页)
g*********s
发帖数: 1782
1
下面这段没问题:
bool op_less(int x, int y) {
return (x < y);
}
bool op_greater(int x, int y) {
return (x > y);
}
bool (*compare_op)(int, int) = is_min ? &op_less : &op_greater;
但是下面这段总是编译失败:
bool (*compare_op)(int, int) = is_min ? &(operator<) : &(operator>);
error: ‘operator<’ not defined
error: ‘operator>’ not defined
这样也不行:
bool (*compare_op)(int, int) = is_min ? &(operator<(int, int)) : &(operator>(int, int));
basic.h:83: error: expected primary-expression before ‘int’
basic.h:83: error: expected primary-expression before ‘int’
basic.h:83: error: ‘operator<’ not defined
basic.h:83: error: expected primary-expression before ‘int’
basic.h:83: error: expected primary-expression before ‘int’
basic.h:83: error: ‘operator>’ not defined
是因为“operator<”实际是个模板,“operator<(int, int)”虽然是个函数,却没有一个具体的地址对应?
如果是我自己定义的模板或特例化的模板行不行?
p***o
发帖数: 1252
2
No such thing, just use op_less and op_greater as you defined.

【在 g*********s 的大作中提到】
: 下面这段没问题:
: bool op_less(int x, int y) {
: return (x < y);
: }
: bool op_greater(int x, int y) {
: return (x > y);
: }
: bool (*compare_op)(int, int) = is_min ? &op_less : &op_greater;
: 但是下面这段总是编译失败:
: bool (*compare_op)(int, int) = is_min ? &(operator<) : &(operator>);

X****r
发帖数: 3557
3
The built-in < operator is not a function nor a function template.
It is an operator. You cannot take address of an operator.
You can declare your own function or function template to overload
the built-in < operator, under the `name' "operation <", in which
case you can take address of this function (or instantiated template)

【在 g*********s 的大作中提到】
: 下面这段没问题:
: bool op_less(int x, int y) {
: return (x < y);
: }
: bool op_greater(int x, int y) {
: return (x > y);
: }
: bool (*compare_op)(int, int) = is_min ? &op_less : &op_greater;
: 但是下面这段总是编译失败:
: bool (*compare_op)(int, int) = is_min ? &(operator<) : &(operator>);

k******r
发帖数: 2300
4
You can do like this,
compare_op = is_min ? op_less : op_greater;
Let me know if you have any issue.

【在 g*********s 的大作中提到】
: 下面这段没问题:
: bool op_less(int x, int y) {
: return (x < y);
: }
: bool op_greater(int x, int y) {
: return (x > y);
: }
: bool (*compare_op)(int, int) = is_min ? &op_less : &op_greater;
: 但是下面这段总是编译失败:
: bool (*compare_op)(int, int) = is_min ? &(operator<) : &(operator>);

k******r
发帖数: 2300
5
No, that is not an operator, it is a function.

【在 X****r 的大作中提到】
: The built-in < operator is not a function nor a function template.
: It is an operator. You cannot take address of an operator.
: You can declare your own function or function template to overload
: the built-in < operator, under the `name' "operation <", in which
: case you can take address of this function (or instantiated template)

d****p
发帖数: 685
6
Someone asked me the difference between operator and function. I don't know
how to answer properly. Anyone with a clue?

【在 g*********s 的大作中提到】
: 下面这段没问题:
: bool op_less(int x, int y) {
: return (x < y);
: }
: bool op_greater(int x, int y) {
: return (x > y);
: }
: bool (*compare_op)(int, int) = is_min ? &op_less : &op_greater;
: 但是下面这段总是编译失败:
: bool (*compare_op)(int, int) = is_min ? &(operator<) : &(operator>);

X****r
发帖数: 3557
7
Where is the function declaration then?

【在 k******r 的大作中提到】
: No, that is not an operator, it is a function.
X****r
发帖数: 3557
8
Err.. they are totally different things. Operators are special
non-alphabetic symbols with their predefined syntax and semantics,
and take one or more operand to form expressions. Functions are
units of code that has its own declaration, body, scope, etc.,
and can be called using the function call operator ().
The only connection between them is that for overloadable operators,
you can define functions with the name "operator @" where @ is the
operator symbol for your own class or enum to overload that operator
to call your function instead.

know

【在 d****p 的大作中提到】
: Someone asked me the difference between operator and function. I don't know
: how to answer properly. Anyone with a clue?

d****p
发帖数: 685
9
Thanks - your summary is really good.
I especially like your highlighting function call operator() - that clearly
shows function is an operand of (the) operator.
operator and operand are of course different.

【在 X****r 的大作中提到】
: Err.. they are totally different things. Operators are special
: non-alphabetic symbols with their predefined syntax and semantics,
: and take one or more operand to form expressions. Functions are
: units of code that has its own declaration, body, scope, etc.,
: and can be called using the function call operator ().
: The only connection between them is that for overloadable operators,
: you can define functions with the name "operator @" where @ is the
: operator symbol for your own class or enum to overload that operator
: to call your function instead.
:

k******r
发帖数: 2300
10
Most likely, the op just posted the part of his code. The declaration of
functions are put in another header file. BTW, if the functions are used
only in one file, then the declaration of functions are not even necessary.

【在 X****r 的大作中提到】
: Where is the function declaration then?
相关主题
string operator +C++命名空间和算子重载
重载 ^ 操作符编译出错请问c++中操作符可以声明为虚函数吗?
overriding operator<<member and friend
进入Programming版参与讨论
k******r
发帖数: 2300
11
Operators are special non-alphabetic symbols? That is not necessarily true.
What about the operator sizeof?

【在 X****r 的大作中提到】
: Err.. they are totally different things. Operators are special
: non-alphabetic symbols with their predefined syntax and semantics,
: and take one or more operand to form expressions. Functions are
: units of code that has its own declaration, body, scope, etc.,
: and can be called using the function call operator ().
: The only connection between them is that for overloadable operators,
: you can define functions with the name "operator @" where @ is the
: operator symbol for your own class or enum to overload that operator
: to call your function instead.
:

X****r
发帖数: 3557
12
Yes, that's not entirely correct. Special non-alphabetic symbols
or keywords, that is.

true.

【在 k******r 的大作中提到】
: Operators are special non-alphabetic symbols? That is not necessarily true.
: What about the operator sizeof?

g*********s
发帖数: 1782
13
No, it's the complete code. But I get your point. You mean if operator< is
defined, then we will be able to get its address.

.

【在 k******r 的大作中提到】
: Most likely, the op just posted the part of his code. The declaration of
: functions are put in another header file. BTW, if the functions are used
: only in one file, then the declaration of functions are not even necessary.

t****t
发帖数: 6806
14
no, if operator< is defined, we won't be able to get its address.

【在 g*********s 的大作中提到】
: No, it's the complete code. But I get your point. You mean if operator< is
: defined, then we will be able to get its address.
:
: .

k******r
发帖数: 2300
15
No, I meant you define a function pointer as,
bool (*compare_op)(int, int);
then you may initialize such a function pointer by using the following
statement,
compare_op = is_min ? op_less : op_greater; (this is what you want, right?)
Now you are free to use compare_op, for example,
bool temp = (*compare_op)(3,1);

【在 g*********s 的大作中提到】
: No, it's the complete code. But I get your point. You mean if operator< is
: defined, then we will be able to get its address.
:
: .

e*****w
发帖数: 144
16
I think there is no user defined function such as "operator<(int, int)".
There has to be at least one user defined type in the function signature in
order to override an operator.
d****p
发帖数: 685
17
Hmm I guess we can get addresses of customized operator functions.

【在 t****t 的大作中提到】
: no, if operator< is defined, we won't be able to get its address.
1 (共1页)
进入Programming版参与讨论
相关主题
*(&b1)=b编译不过,b1=b可以,区别是?问个c++的template的问题
override operator[] inlinestring operator +
今天给c++震惊了重载 ^ 操作符编译出错
leetcode上一个问题请教overriding operator<<
面试问题C++命名空间和算子重载
问个c++的弱问题请问c++中操作符可以声明为虚函数吗?
C语言里面的register变量能否进行取地址操作? (转载)member and friend
0 < -1 ? A c++ question能否对某个库进行操作符重载?
相关话题的讨论汇总
话题: operator话题: int话题: op话题: function话题: defined