473,842 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

universal selector

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<typena me 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
Jul 22 '05 #1
11 1711
On 26 Nov 2004 07:45:30 -0800, vl*********@yah oo.com (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<typen ame 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
No**********@Ho me.com
Jul 22 '05 #2
* 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<typena me 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<typena me 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?
Jul 22 '05 #3
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<typena me 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<typena me 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)();
}
Jul 22 '05 #4
vl*********@yah oo.com (Vladimir) wrote in message news:<a3******* *************** ****@posting.go ogle.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<typena me 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<typena me T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}

class test
{
int val;
public:
test(int inVal):val(inVa l) {}
int testFunc() {return val;}
};
int main(int argc, char* argv[])
{
test one(1);
test two(2);

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

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

return 0;
}

Output
1
2

Ok, I'm not quite getting the problem.
Socks
Jul 22 '05 #5
* pu*********@hot mail.com:
vl*********@yah oo.com (Vladimir) wrote in message news:<a3******* *************** ****@posting.go ogle.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<typena me 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<typena me T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}

class test
{
int val;
public:
test(int inVal):val(inVa l) {}
int testFunc() {return val;}
};
int main(int argc, char* argv[])
{
test one(1);
test two(2);

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

std::cout << select(1,one.te stFunc() ,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?
Jul 22 '05 #6
Bob Hairgrove <in*****@bigfoo t.com> 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.somefunctio n0, obj.somefunctio n1)(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
Jul 22 '05 #7
Gianni Mariani <gi*******@mari ani.ws> 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
Jul 22 '05 #8
On 27 Nov 2004 07:51:20 -0800, vl*********@yah oo.com (Vladimir) wrote:
Bob Hairgrove <in*****@bigfoo t.com> 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.somefunctio n0, obj.somefunctio n1)(args...)

doesn't work as expected?


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

(obj.*(select(i ndex, 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
No**********@Ho me.com
Jul 22 '05 #9
vl*********@yah oo.com (Vladimir) wrote:
Gianni Mariani <gi*******@mari ani.ws> 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.
Jul 22 '05 #10

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

Similar topics

0
1882
by: Nick Kew | last post by:
Rationale ========= Many applications today benefit from an SGML and/or XML Entity Catalogue to dereference entities referenced by a Public Identifier. For a validating SGML parser this is an absolute requirement. For any SGML or XML parser it serves to enable entities such as DTDs and modules to be resolved locally. Hitherto, different packages and applications have distributed entity
33
2730
by: news.microsoft.com | last post by:
To Microsoft and fellow MSDN Universal subscribers... Regarding new MSDN Universal (I mean Premier) price and level changes: 1) Way too expensive for the small and medium developer Universal subscriber (and some large ones as well). $10,000 - $15,000 per user?!? Forget it! 2) Do you (MS) honestly believe that the market you are targeting will just buy this product on good faith? Without our recommendations? Think about
2
2155
by: Chris Sharman | last post by:
See http://services.ccagroup.co.uk/testform.htm Looks as intended in firefox, is valid (bulk of inputs centred in a div occupying the left half of the page). ie ignores the child selector, positioning everything aligned left. I'm familiar with http://w3development.de/css/hide_css_from_browsers/ I thought the ie child selector bug only occurred where there were no spaces (parent>child) - I'd thought it worked when there were (parent >
8
2343
by: sajid | last post by:
The CSS 2.1 Specification describes how to sort a list of selectors in order of specificity, but it doesn't provide a method to calculate the specificity of a single selector in isolation. I've devised a method to do this, which I describe in the following article: http://calculating-css-selector-specificity.blogspot.com/ Comments and/or criticisms welcome.
3
1940
by: JakDaniel | last post by:
is it possible, create a selector as alias of another selector... (maybe) in another stylesheet file? ex: html page .... <link rel="stylesheet" type="text/css" href="style1.css" /> <link rel="stylesheet" type="text/css" href="style2.css" /> ....
6
7200
by: _googlepost | last post by:
I have some CSS that goes something like this: table.TableStyle {font-family: "Helvetica", "Ariel"; background- color:white; border-collapse:collapse;} table.TableStyle COLGROUP { background- color:lavender;} The second line is a style that is applied to a particular named COLGROUP element in tables assigned the TableStyle class. Unfortunately, while this works in most modern browsers (including
1
1581
by: PaulShimm | last post by:
I have this simple div with a label in: <div id="TestDiv"> <label>TEST CSS</label> </div> I am applying the following CSS: div#TestDiv *
0
9875
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9715
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10945
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10614
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10674
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10317
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9454
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5697
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3146
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.