0

I have two containers with the type of A

A<string, double> s1;
A<string, bool> s2;

So I wrote the A class with the necessary functions and I used a

template<class T, class E>

for it. It has a function that returns a value of E, in my case, it returns a double which I'm trying to compare to another double. I'm supposed to overload the < operator for that, but I have no idea how to do that.

4.5 < s1.at("a");

s1.at("a"); returns 1.5, so it's false. I need it to return true.
I thought since they are both double values, I can just

bool operator< (double& rhs) {
    return rhs < *this;
}

But it's not working. I also thought it should be friend with two parameters, but it just doesn't make sense since the rhs is a double returned by a function, not a type of A.
So I guess it really is two different types and that's why my first idea is not working.

2
Contributors
1
Reply
25
Views
4 Years
Discussion Span
Last Post by sepp2k
0

You can not change the behaviour of < being used with two doubles. The only thing you could do is to make at return a custom type instead of E, like a Proxy<E>. Then you could define static bool operator<(E, Proxy<E>) and that one will be called when you do 4.5 < s1.at("a").

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.