| re: Help Required: Operator overloaded function template and Friend
Thank you for your help. I did think about making it a member
function, but I was hoping to take advantage of implicit conversions.
In fact, what I didn't show in my original posting was another problem
where I couldn't get the implicit conversion work. It's more likely
below where there are two nested
classes and there is an implicit conversion from Sub2 to Sub1, but not
vice versa. And I thought by making it a global function template, I
could take advantage of the implicit conversion as well, but it
doesn't work on 3.4.2 either.
template <typename T>
class Obj {
public:
class Sub1;
class Sub2;
};
Thank you for your tip on using the functional notation, and probably
I will fix my problem that way until I find a better solution, but it
does seem to defeat the purpose of using operator overloading in the
first place.
I hope to get more insight to the problem by asking following
questions if you don't mind. I apologise in advance if this is not the
right group for this kind of questions.
1. It used to work with gcc 3.2.3 but not with 3.4.2 anymore. What is
the correct behaviour according to the standard? What is the rule
governing "deducible contexts" that I should study? I also wonder what
other compilers do with this situation since I have only gcc.
2. If it's correct not to be able to deduce T in this case according
to the standard, then any function template which accepts a nested
type of a class template whose type parameter needs to be deduced from
the funtion call arguments would never work, would it? Am I
understanding correctly?
Can some experts kindly provide answers to my questions above? Thank
you very much in advance.
[color=blue]
> I think you've run into one of those "non-deducible contexts". The T is
> too deeply entrenched into the 'a' and 'b' arguments here for the compiler
> to figure out from real arguments (operands) you give it.
>[color=green]
> > [..]
> > #include <iostream>
> > int main()
> > {
> > Obj<int>::Sub a(10),b(10);
> > if ( a == b )
> > std::cout << "OK" ;
> > }[/color]
>
> There are probably several ways to fix it and one would be to make your
> operator== a member:
>
> template <typename T>
> class Obj<T>::Sub {
> public:
> Sub(int i):n(i) { }
> bool operator==(const Obj<T>::Sub &b) const
> {
> return n == b.n;
> }
> private:
> int n;
> };
>
> Another way is to call the operator function using the functional notation
> with the template argument explicit:
>
> ...
> if (operator==<int>(a,b))
> ...
>
> V[/color] |