By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

Problem with function selection in templates

P: n/a
Hi there.

I have some template functions that operate on different types of object. If
the object has a certain memberfunction I want to call that function, and if
not, I want to call a global function that operates on that object.

Some pseudocode to show what I mean:

template<class Tvoid Function(T& MyObject)
{
if exist MyObject.Process() then MyObject.Process();
else GlobalProcess(MyObject);
}

is there a good way to do this without having to write a lot of overloaded
functions for one of the cases?

Thanks in advance
Pär Buschka

Nov 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Pär Buschka wrote:
I have some template functions that operate on different types of
object. If the object has a certain memberfunction I want to call
that function, and if not, I want to call a global function that
operates on that object.
Some pseudocode to show what I mean:

template<class Tvoid Function(T& MyObject)
{
if exist MyObject.Process() then MyObject.Process();
else GlobalProcess(MyObject);
}

is there a good way to do this without having to write a lot of
overloaded functions for one of the cases?
template<class T, bool usememberstruct ObjectProcessor;

template<class Tstruct ObjectProcessor<T, true{
void operator()(T& MyObject) {
MyObject.Process();
}
};

template<class Tstruct ObjectProcessor<T, false{
void operator()(T& MyObject) {
GlobalProcess(MyObject);
}
};

template<class Tvoid Function(T& MyObject)
{
ObjectProcessor<T, has_member< ??? ()(MyObject);
}

Look up a suitable implementation of "has_member" on the Web.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 6 '06 #2

P: n/a
Thank you very much for the help, it seems to work perfect.

A suitable has_member function can for instance be found at:
http://www.tech-archive.net/Archive/...4-10/0007.html

/Pär

"Victor Bazarov" <v.********@comAcast.netskrev i meddelandet
news:ei**********@news.datemas.de...
Pär Buschka wrote:
>I have some template functions that operate on different types of
object. If the object has a certain memberfunction I want to call
that function, and if not, I want to call a global function that
operates on that object.
Some pseudocode to show what I mean:

template<class Tvoid Function(T& MyObject)
{
if exist MyObject.Process() then MyObject.Process();
else GlobalProcess(MyObject);
}

is there a good way to do this without having to write a lot of
overloaded functions for one of the cases?

template<class T, bool usememberstruct ObjectProcessor;

template<class Tstruct ObjectProcessor<T, true{
void operator()(T& MyObject) {
MyObject.Process();
}
};

template<class Tstruct ObjectProcessor<T, false{
void operator()(T& MyObject) {
GlobalProcess(MyObject);
}
};

template<class Tvoid Function(T& MyObject)
{
ObjectProcessor<T, has_member< ??? ()(MyObject);
}

Look up a suitable implementation of "has_member" on the Web.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Nov 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.