Programming, for all ages and all languages.
Alboin
Member
Posts: 1466 Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia
Post
by Alboin » Sun Aug 26, 2007 5:05 pm
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++...
C8H10N4O2 | #446691 | Trust the nodes.
os64dev
Member
Posts: 553 Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands
Post
by os64dev » Mon Aug 27, 2007 12:49 am
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.
Alboin
Member
Posts: 1466 Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia
Post
by Alboin » Mon Aug 27, 2007 6:20 am
os64dev wrote: I have to ask what is the type of string?.
It's a standard C++ string. (ie. using namespace std.)
C8H10N4O2 | #446691 | Trust the nodes.
os64dev
Member
Posts: 553 Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands
Post
by os64dev » Mon Aug 27, 2007 6:22 am
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;
Alboin
Member
Posts: 1466 Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia
Post
by Alboin » Mon Aug 27, 2007 6:32 am
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...
C8H10N4O2 | #446691 | Trust the nodes.
jnc100
Member
Posts: 775 Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:
Post
by jnc100 » Mon Aug 27, 2007 7:22 am
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.
Alboin
Member
Posts: 1466 Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia
Post
by Alboin » Mon Aug 27, 2007 7:52 am
Hey, thanks, that works.
However, is there anyway to call it normally?
C8H10N4O2 | #446691 | Trust the nodes.
jnc100
Member
Posts: 775 Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:
Post
by jnc100 » Mon Aug 27, 2007 8:30 am
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.
Alboin
Member
Posts: 1466 Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia
Post
by Alboin » Mon Aug 27, 2007 1:44 pm
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.
C8H10N4O2 | #446691 | Trust the nodes.
Candy
Member
Posts: 3882 Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven
Post
by Candy » Tue Aug 28, 2007 10:41 am
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.