Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with function selection in templates

Reply
Thread Tools

Problem with function selection in templates

 
 
Pär Buschka
Guest
Posts: n/a
 
      11-06-2006
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 T> void 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



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-06-2006
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 T> void 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 usemember> struct ObjectProcessor;

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

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

template<class T> void 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


 
Reply With Quote
 
 
 
 
Pär Buschka
Guest
Posts: n/a
 
      11-07-2006
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" <(E-Mail Removed)> skrev i meddelandet
news:eio2l3$np0$(E-Mail Removed)...
> 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 T> void 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 usemember> struct ObjectProcessor;
>
> template<class T> struct ObjectProcessor<T, true> {
> void operator()(T& MyObject) {
> MyObject.Process();
> }
> };
>
> template<class T> struct ObjectProcessor<T, false> {
> void operator()(T& MyObject) {
> GlobalProcess(MyObject);
> }
> };
>
> template<class T> void 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Class templates and friend function templates BigMan C++ 1 07-23-2005 09:24 PM
JTable with row selection, but no cell selection Simon Niederberger Java 2 01-07-2005 04:17 PM
JS comparing innerHTML to text selection (window.getSelection() /document.selection) Andrew Crowe HTML 1 09-13-2004 02:22 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments