h**o 发帖数: 347 | 1 原例抄自于Bjarne的书p 275,让自己定义的类参于已存在的类的运算
代码如H下 | h**o 发帖数: 347 | 2 re啊,问题是长了一点。。。
【在 h**o 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 原例抄自于Bjarne的书p 275,让自己定义的类参于已存在的类的运算 : 代码如H下
| g***e 发帖数: 577 | 3 I read it, but I dont know the answer.:)
【在 h**o 的大作中提到】![](/moin_static193/solenoid/img/up.png) : re啊,问题是长了一点。。。
| X****r 发帖数: 3557 | 4 In case of a function template, template argument deduction is performed
before the compiler tries to resolve the overloading [13.3.1 paragraph 7].
In other words, the compiler sees "a*b", then it looks for all overloaded "
operator*". You have "template operator *(const A&, const A&)
;" defined up there, so it tries to deduct the typename T. Note that the
compiler doesn't look into anything else in this step, including your user-
defined conversion function. Obviously it cannot f | h**o 发帖数: 347 | 5 thx
【在 g***e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I read it, but I dont know the answer.:)
| h**o 发帖数: 347 | 6 谢谢,我明白这层意思了
之所以我想用implicit conversion,就是因为class A已经有很多运算了
一个一个再重写既麻烦,我又不知道是不是多余。。。
&)
so
all
【在 X****r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : In case of a function template, template argument deduction is performed : before the compiler tries to resolve the overloading [13.3.1 paragraph 7]. : In other words, the compiler sees "a*b", then it looks for all overloaded " : operator*". You have "template operator *(const A&, const A&) : ;" defined up there, so it tries to deduct the typename T. Note that the : compiler doesn't look into anything else in this step, including your user- : defined conversion function. Obviously it cannot f
| h**o 发帖数: 347 | 7 I get most of your points. But could you belabor a little on your last
sentence please?
The problem I have is that class A is actually built-in in STL and it has
a lot of non-member binary operations. Class B is defined by me. It is a
variant of class A. What I am looking for is a way to implicitly convert
B to A when invoking any function defined for A.
I don't know if this kind of shortcut exits. It is impossible to define a
base class and it looks verbose to define all combinations of A & B f
【在 X****r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : In case of a function template, template argument deduction is performed : before the compiler tries to resolve the overloading [13.3.1 paragraph 7]. : In other words, the compiler sees "a*b", then it looks for all overloaded " : operator*". You have "template operator *(const A&, const A&) : ;" defined up there, so it tries to deduct the typename T. Note that the : compiler doesn't look into anything else in this step, including your user- : defined conversion function. Obviously it cannot f
|
|