Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > universal selector

Reply
Thread Tools

universal selector

 
 
Vladimir
Guest
Posts: n/a
 
      11-26-2004
Hello!

I'd like to select one of possible entities according to a given index:

select(index, A, B)

would return A if index is 0, B if index is (or greater than) 1.

so i write:

template<typename T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}

this works fine when calling selected function:

select(index, somefunction0, somefunction1)(args...)

but fails for class member functions - what should I do?
Ideally I want it universal, without the need
to rewrite select() for each class.

--
Vladimir
 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      11-26-2004
On 26 Nov 2004 07:45:30 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (Vladimir) wrote:

>Hello!
>
>I'd like to select one of possible entities according to a given index:
>
>select(index, A, B)
>
>would return A if index is 0, B if index is (or greater than) 1.
>
>so i write:
>
>template<typename T> T select(int index, T a, T b)
>{
> return (index == 0) ? a : b;
>}
>
>this works fine when calling selected function:
>
>select(index, somefunction0, somefunction1)(args...)
>
>but fails for class member functions - what should I do?
>Ideally I want it universal, without the need
>to rewrite select() for each class.


You cannot call non-static member functions without an object.

--
Bob Hairgrove
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-26-2004
* Vladimir:
> Hello!
>
> I'd like to select one of possible entities according to a given index:
>
> select(index, A, B)
>
> would return A if index is 0, B if index is (or greater than) 1.
>
> so i write:
>
> template<typename T> T select(int index, T a, T b)
> {
> return (index == 0) ? a : b;
> }
>
> this works fine when calling selected function:
>
> select(index, somefunction0, somefunction1)(args...)
>
> but fails for class member functions - what should I do?
> Ideally I want it universal, without the need
> to rewrite select() for each class.


#include <iostream>
#include "generalmaker.h"
#include <string>
#include <list>

using namespace std;

#include <iostream>

template<typename T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}

class X
{
public:
void a( char const s[] ) const
{
std::cout << "a: " << s << std::endl;
}

void b( char const s[] ) const
{
std::cout << "b: " << s << std::endl;
}
};

int main()
{
X o;

(o.*select( 0, X::a, X::b ))( "Silly syntax, yes." );
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      11-26-2004
Vladimir wrote:
> Hello!
>
> I'd like to select one of possible entities according to a given index:
>
> select(index, A, B)
>
> would return A if index is 0, B if index is (or greater than) 1.
>
> so i write:
>
> template<typename T> T select(int index, T a, T b)
> {
> return (index == 0) ? a : b;
> }
>
> this works fine when calling selected function:
>
> select(index, somefunction0, somefunction1)(args...)
>
> but fails for class member functions - what should I do?
> Ideally I want it universal, without the need
> to rewrite select() for each class.


The code below compiled fine. What exactly is your problem ?

template<typename T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}

struct X
{
void f1() {}
void f2() {}
};

int main()
{
void (X::*z)() = select( 0, &X::f1, &X::f2 );

X q;

(q.*(select( 1, &X::f1, &X::f2 )))();
(q.*z)();
}
 
Reply With Quote
 
puppet_sock@hotmail.com
Guest
Posts: n/a
 
      11-26-2004
(E-Mail Removed) (Vladimir) wrote in message news:<(E-Mail Removed). com>...
> Hello!
>
> I'd like to select one of possible entities according to a given index:
>
> select(index, A, B)
>
> would return A if index is 0, B if index is (or greater than) 1.
>
> so i write:
>
> template<typename T> T select(int index, T a, T b)
> {
> return (index == 0) ? a : b;
> }
>
> this works fine when calling selected function:
>
> select(index, somefunction0, somefunction1)(args...)
>
> but fails for class member functions - what should I do?
> Ideally I want it universal, without the need
> to rewrite select() for each class.


#include <iostream>

template<typename T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}

class test
{
int val;
public:
test(int inVal):val(inVal) {}
int testFunc() {return val;}
};


int main(int argc, char* argv[])
{
test one(1);
test two(2);

std::cout << select(0,one.testFunc() ,two.testFunc() ) << std::endl ;

std::cout << select(1,one.testFunc() ,two.testFunc() ) << std::endl ;

return 0;
}

Output
1
2

Ok, I'm not quite getting the problem.
Socks
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-26-2004
* (E-Mail Removed):
> (E-Mail Removed) (Vladimir) wrote in message news:<(E-Mail Removed). com>...
> > Hello!
> >
> > I'd like to select one of possible entities according to a given index:
> >
> > select(index, A, B)
> >
> > would return A if index is 0, B if index is (or greater than) 1.
> >
> > so i write:
> >
> > template<typename T> T select(int index, T a, T b)
> > {
> > return (index == 0) ? a : b;
> > }
> >
> > this works fine when calling selected function:
> >
> > select(index, somefunction0, somefunction1)(args...)
> >
> > but fails for class member functions - what should I do?
> > Ideally I want it universal, without the need
> > to rewrite select() for each class.

>
> #include <iostream>
>
> template<typename T> T select(int index, T a, T b)
> {
> return (index == 0) ? a : b;
> }
>
> class test
> {
> int val;
> public:
> test(int inVal):val(inVal) {}
> int testFunc() {return val;}
> };
>
>
> int main(int argc, char* argv[])
> {
> test one(1);
> test two(2);
>
> std::cout << select(0,one.testFunc() ,two.testFunc() ) << std::endl ;
>
> std::cout << select(1,one.testFunc() ,two.testFunc() ) << std::endl ;
>
> return 0;
> }
>
> Output
> 1
> 2
>
> Ok, I'm not quite getting the problem.


Each of your select calls makes two function calls for the arguments.

The OP wants just one, namely a call of the selected function.

That's easily achieved, as Gianni Mariani and I have shown earlier.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Vladimir
Guest
Posts: n/a
 
      11-27-2004
Bob Hairgrove <(E-Mail Removed)> wrote:
> >this works fine when calling selected function:
> >
> >select(index, somefunction0, somefunction1)(args...)
> >
> >but fails for class member functions - what should I do?
> >Ideally I want it universal, without the need
> >to rewrite select() for each class.

>
> You cannot call non-static member functions without an object.


so, why

select(index, obj.somefunction0, obj.somefunction1)(args...)

doesn't work as expected?

Why there is so much difference between functions and
member functions? Aren't templates supposed to be matched
to any type? Is it compiler implementation issue?

--
Vladimir
 
Reply With Quote
 
Vladimir
Guest
Posts: n/a
 
      11-27-2004
Gianni Mariani <(E-Mail Removed)> wrote:
> >
> > select(index, somefunction0, somefunction1)(args...)
> >

>
> The code below compiled fine. What exactly is your problem ?
>
> (q.*(select( 1, &X::f1, &X::f2 )))();


My problem is that we usually write helper functions
to simplify coding and improve readability.
This also means less typing which unfortunately
isn't the case with the above solution.

Well this code works, but instead of quickly selecting
and calling member function we have to do scope resolution,
address taking, dereferencing and lots of brackets...

--
Vladimir
 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      11-28-2004
On 27 Nov 2004 07:51:20 -0800, (E-Mail Removed) (Vladimir) wrote:

>Bob Hairgrove <(E-Mail Removed)> wrote:
>> >this works fine when calling selected function:
>> >
>> >select(index, somefunction0, somefunction1)(args...)
>> >
>> >but fails for class member functions - what should I do?
>> >Ideally I want it universal, without the need
>> >to rewrite select() for each class.

>>
>> You cannot call non-static member functions without an object.

>
>so, why
>
>select(index, obj.somefunction0, obj.somefunction1)(args...)
>
>doesn't work as expected?


It's simply the wrong syntax for calling pointer to member. Try this:

(obj.*(select(index, somefunction0, somefunction1)))(args...)

>Why there is so much difference between functions and
>member functions? Aren't templates supposed to be matched
>to any type? Is it compiler implementation issue?


As you can see, everything inside the brackets beginning with
"select(" is just the way you had it. Templates work fine. It's
afterwards where you have trouble. RTFM about pointer to member. They
are actually a kind of offset into the class function list, not real
pointers.

Using an index to select functions seems to me not very
object-oriented. Maybe you could rethink the design and use virtual
functions instead.

--
Bob Hairgrove
(E-Mail Removed)
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      11-28-2004
(E-Mail Removed) (Vladimir) wrote:
> Gianni Mariani <(E-Mail Removed)> wrote:
> > >
> > > select(index, somefunction0, somefunction1)(args...)
> > >

> >
> > The code below compiled fine. What exactly is your problem ?
> >
> > (q.*(select( 1, &X::f1, &X::f2 )))();

>
> My problem is that we usually write helper functions
> to simplify coding and improve readability.
> This also means less typing which unfortunately
> isn't the case with the above solution.
>
> Well this code works, but instead of quickly selecting
> and calling member function we have to do scope resolution,
> address taking, dereferencing and lots of brackets...


Then, don't use silly obfuscations like this select() function.
It might save you a couple of characters from time to time but
it makes the code harder to read for anybody who doesn't know
what the function does (or even yourself, if you come back to
it in future and forget what the select criterion was). FWIW
it also collides with the POSIX 'select' function, if you ever
port your code to Unix systems.
 
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
Universal Selector to gain browser uniformity Jan Clemens Faerber HTML 4 04-05-2013 06:36 AM
Sigma Announces SD-30--30 Megapixel,Universal Lens Mount, Digital SLRSigma Announces SD-30--30 Megapixel,Universal Lens Mount, Digital SLR sigmaphotojapan@yahoo.com Digital Photography 6 04-01-2005 05:26 PM
Sigma Announces SD-30--30 Megapixel,Universal Lens Mount, Digital SLRSigma Announces SD-30--30 Megapixel,Universal Lens Mount, Digital SLR sigmaphotojapan@yahoo.com Digital Photography 5 04-01-2005 02:08 PM
warning message for case statements where the selector signal is of type std_logic_vector profpenguin@shaw.ca VHDL 6 02-11-2005 05:38 AM
Color selector Chris ASP .Net 1 12-10-2003 02:42 PM



Advertisments