Page 1 of 1
Overloading Operators and Templates in C++
Posted: Sun Aug 26, 2007 5:05 pm
by Alboin
Hello,
I have a class that overloads the [] operator. The overloading function is:
Code: Select all
template <class C>
C &operator [](string key) {
return static_cast<C>(data);
}
However, I have been unable to call said overloader with:
(A being an instance of the class.)
Instead, I get:
Code: Select all
reg.cc:27: error: expected primary-expression before 'int'
reg.cc:27: error: expected `;' before 'int'
Anyone know how to call it? It does compile without the call, so I think it's legal C++...
Posted: Mon Aug 27, 2007 12:49 am
by os64dev
I have to ask what is the type of string?.
Code: Select all
template <class C>
C &operator [](string key) {
return static_cast<C>(data);
}
A<int>["AX"] = 12;
In his example would be translated to 'A<int>& operator[](const char *)'.
I'll look into it a bit more and give an update.
update:
Code: Select all
#include <stdio.h>
typedef const char * string;
template <typename T>
class A {
public:
T & operator [](string key) {
printf("using index: '%s'\n", key);
return data;
}
private:
T data;
};
int main(void) {
A<int> x;
x["AX"] = 12;
int w = x["ax"];
}
this works. but the templated version of the operator [] doesn't seem to work.
Posted: Mon Aug 27, 2007 6:20 am
by Alboin
os64dev wrote:I have to ask what is the type of string?.
It's a standard C++ string. (ie. using namespace std.)
Posted: Mon Aug 27, 2007 6:22 am
by os64dev
Alboin wrote:
It's a standard C++ string. (ie. using namespace std.)
then this should work. not sure though.
Code: Select all
A<int> x;
x[std:string("AX")] = 12;
Posted: Mon Aug 27, 2007 6:32 am
by Alboin
I still get the expected primary-expression error. Here, this is the entire class:
Code: Select all
template <class T>
class Register {
public:
Register(char name) {
}
template <class C>
C &operator [](string key) {
}
};
Template C is a function template, but I don't know how to call [] with it...
Posted: Mon Aug 27, 2007 7:22 am
by jnc100
What happens if you try calling the operator function like:
Code: Select all
Register<T> a;
int b;
b = a.operator[]<int>("AX");
If that doesn't work it should give you a slightly more helpful error message.
Regards,
John.
Posted: Mon Aug 27, 2007 7:52 am
by Alboin
Hey, thanks, that works.
However, is there anyway to call it normally?
Posted: Mon Aug 27, 2007 8:30 am
by jnc100
According to
http://www.thescripts.com/forum/thread461406.html there doesn't seem to be. There are some ways around it, however:
1) Define a member function
Code: Select all
template <class C> C& Item(string key)
and use that instead of operator overloading.
2) Define the class as
Code: Select all
template <class T, class C> {
...
C& operator[](string key)
although this limits you to using only one return type for operator[] for the entire lifetime of your class.
Regards,
John.
Posted: Mon Aug 27, 2007 1:44 pm
by Alboin
jnc100 wrote:According to
http://www.thescripts.com/forum/thread461406.html there doesn't seem to be. There are some ways around it, however:
1) Define a member function
Code: Select all
template <class C> C& Item(string key)
and use that instead of operator overloading.
2) Define the class as
Code: Select all
template <class T, class C> {
...
C& operator[](string key)
although this limits you to using only one return type for operator[] for the entire lifetime of your class.
Regards,
John.
That's what I feared. 'Means I'm going top have to change my plans....
I wonder why it isn't allowed. Maybe they'll fix it in the future....
Thanks again.
Posted: Tue Aug 28, 2007 10:41 am
by Candy
Alboin wrote:
That's what I feared. 'Means I'm going top have to change my plans....
I wonder why it isn't allowed. Maybe they'll fix it in the future....
Doubt it. Operator overloading is intended for cases in which you can easily see why you use the given operator because it's really intuitive. If you have to specify the type of return value somehow it's not intuitive anymore, so I can figure you can't call it using the inline syntax then.