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?
| | | 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.
|
|