Overloading Operators and Templates in C++

Programming, for all ages and all languages.
Post Reply
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Overloading Operators and Templates in C++

Post 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:

Code: Select all

A<int>["AX"] = 12;
(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.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post 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.
Author of COBOS
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

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.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post 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;
Author of COBOS
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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...
C8H10N4O2 | #446691 | Trust the nodes.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post 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.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Hey, thanks, that works. :)

However, is there anyway to call it normally?
C8H10N4O2 | #446691 | Trust the nodes.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post 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.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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.
Post Reply