473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

templating operator[]

frs
It is obviously possible to template the operator[]. But
how can one use it? Considering the following example,
how would I get something like 'a<int>[12]', assuming
'a' is of type 'A'. Is it my compiler that is too weak
(g++ 3.3.5) ?

Thanks and Regards

Frank

EXAMPLE:----------------------------------------------------------------
struct A {
template <typename T>
int operator[](const int&);
};

template <> int
operator[]<int>(const int& X)
{ return 0; }

template <> int
operator[]<double>(const int& X)
{ return 1; }

Aug 28 '05 #1
3 1220
frs wrote:
It is obviously possible to template the operator[]. But
how can one use it? Considering the following example,
how would I get something like 'a<int>[12]', assuming
'a' is of type 'A'. Is it my compiler that is too weak
(g++ 3.3.5) ?


Does this help ?

struct A {
template <typename T>
int operator[](const int&);
};

template <>
int A::operator[]<int>(const int& X)
{ return 0; }

template <>
int A::operator[]<double>(const int& X)
{ return 1; }
int main()
{
A a;

a.operator[]<int>( 2 );
a.operator[]<double>( 2 );

a[1]; // illegal
}
Aug 28 '05 #2
Gianni, This doesn't compile on MSVC7.1. Does it compile on g++ 3.3.5?
Thank you.

Aug 29 '05 #3
Frank Chang wrote:
Gianni, This doesn't compile on MSVC7.1. Does it compile on g++ 3.3.5?
Thank you.


What errors do you get ? It does compiler (except for the line marked
illegal) on gcc 4.0.0.
Aug 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Leif K-Brooks | last post by:
I'm planning to start on a fairly large web application, most likely using mod_python. I recently wrote a fairly small (but real-world useful) web app with it, and all of those req.write()s made...
7
by: Ksenia Marasanova | last post by:
Hi, I am looking for fast, simple templating system that will allow me to do the following: - embed Python code (or some templating code) in the template - generate text output (not only...
2
by: Matt Davis | last post by:
Hello, I am attempting to create a vector of trees that contain user defined objects. For example, the follwing code will not compile. vector <tree <userObject> > vectorTree; Is this kind...
18
by: pkassianidis | last post by:
Hello everybody, I am in the process of writing my very first web application in Python, and I need a way to generate dynamic HTML pages with data from a database. I have to say I am...
7
by: Michal Nazarewicz | last post by:
Let say I have a class template Set which represent set of objects of given type, and a struct template Pair representing an ordered pair, ie: #v+ template<class T> class Set { /* ......
0
by: Chris Withers | last post by:
Hi All, I'm proud to announce that after almost a year's development, the first public release of Twiddler is available! Twiddler is a simple but flexible templating system for dynamically...
7
by: Paul | last post by:
I have been coding apps with PHP for several years. But I am getting more involved in larger and more complicated PHP applications and want to use best practices. I use Eclipse's PHP IDE. I...
5
by: jmDesktop | last post by:
Hi, I was using .net and it uses templates. I like that it did not mix the UI with logic. I saw there are many templating engines for php usage. Can you recommend one? I don' t know which to...
10
by: Terrence Brannon | last post by:
Hello, The most common way of dynamically producing HTML is via template engines like genshi, cheetah, makotemplates, etc. These engines are 'inline' --- they intersperse programming...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.