C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can operator-> be a template function?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
J W
Guest





PostPosted: Fri Oct 28, 2005 4:04 pm    Post subject: Can operator-> be a template function? Reply with quote



Is it legal for operator-> to be a template function? And if so, how do
you
specify the template parameter?

Example:

struct foo
{
template<int i>
foo* operator->() { return this; }
void myfunc() { }
};

foo f;
f-><1>myfunc();

This is a contrived example, in reality i want to have operator-> be a
template function and have specializations on the int param so i can
dictate
different behavior.

GCC 3.4.4 parses the templated operator-> but I cannot figure out how to
call it, as doing f-><1>myfunc(); does not work.

Jeff


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
acehreli@yahoo.com
Guest





PostPosted: Sat Oct 29, 2005 7:39 pm    Post subject: Re: Can operator-> be a template function? Reply with quote



J W wrote:
Quote:
Is it legal for operator-> to be a template function? And if so, how do
you
specify the template parameter?

Example:

struct foo
{
template<int i
foo* operator->() { return this; }
void myfunc() { }
};

foo f;
f-><1>myfunc();

This is a contrived example, in reality i want to have operator-> be a
template function and have specializations on the int param so i can
dictate
different behavior.

I seriously think that there are better ways of doing what you actually
need, but here is what I was able to run:

#include <iostream>

using std::cout;

struct foo
{
template<int i>
foo * operator->()
{
cout << "defaultn";
return this;
}

void my_func()
{}

int i_;
char c_;
};

template <>
foo * foo::operator-> <42> ()
{
cout << "forty twon";
return this;
}

template <>
foo * foo::operator-> <7> ()
{
cout << "sevenn";
return this;
}


int main()
{
foo f;
foo * p = &f;
p->my_func();
p->operator-><42>()->my_func();
p->operator-><7>()->my_func();
p->operator-><0>()->my_func();
}

Ali


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
jeff_d_harper@hotmail.com
Guest





PostPosted: Sat Oct 29, 2005 7:43 pm    Post subject: Re: Can operator-> be a template function? Reply with quote



Quote:
GCC 3.4.4 parses the templated operator-> but I cannot figure out how to

call it f.operator-><3>()->myfunc();

:-(


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Allan W
Guest





PostPosted: Sat Oct 29, 2005 7:45 pm    Post subject: Re: Can operator-> be a template function? Reply with quote

J W wrote:
Quote:
Is it legal for operator-> to be a template function?

Yes, I think so.

Quote:
And if so, how do you specify the template parameter?

struct foo {
template<int i
foo* operator->() { return this; }
void myfunc() { }
};

foo f;
f-><1>myfunc(); // Doesn't work

This seems to work, at least on Visual Studio .Net 2003:
f.operator-> <1> () -> myfunc();


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bronek Kozicki
Guest





PostPosted: Sun Oct 30, 2005 3:01 pm    Post subject: Re: Can operator-> be a template function? Reply with quote

J W wrote:
Quote:
This is a contrived example, in reality i want to have operator-> be a
template function and have specializations on the int param so i can
dictate different behavior.

As you already know, the syntax to call specialized operator is quite
ugly. If you do not insists on using numbers, you might consider
following syntax "f(foo::gaa)->myfunc();". Full example follows.


B.


#include<cstdio>

// foo.hpp
class foo
{
struct gaa_t {};
struct bar_t {};

public:
static const gaa_t * const gaa;
static const bar_t * const bar;

foo* operator()(const gaa_t *);
foo* operator()(const bar_t *);

void myfunc() { }
};

// foo.cpp
// include "foo.hpp"
foo* foo::operator()(const gaa_t *)
{std::puts("gaa"); return this;}

foo* foo::operator()(const bar_t *)
{std::puts("bar"); return this;}

const foo::gaa_t * const foo::gaa = 0;
const foo::bar_t * const foo::bar = 0;

// main.cpp
// include "foo.hpp"
int main()
{
foo f;
f(foo::gaa)->myfunc();
f(foo::bar)->myfunc();
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.