Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Logisk operator som funksjonsargument?

10 views
Skip to first unread message

AB

unread,
May 6, 2009, 12:25:48 PM5/6/09
to

Er det i det hele tatt mulig � angi en logis operator som argument til
en funksjon p� en eller annen m�te (eller m� jeg ty til Lisp)?

Pseudoeks:

bool foo(void* op, int val1, int val2)
{
return (val1 op val2);
}

if ( foo(<, 1,2))
{
cout << " 1 er mindre enn 2" << endl;
}

________
HL

AB

unread,
May 6, 2009, 12:26:53 PM5/6/09
to

AB

unread,
May 6, 2009, 12:30:02 PM5/6/09
to

Pseudoeks:

______
AB

Alf P. Steinbach

unread,
May 6, 2009, 2:07:54 PM5/6/09
to
* AB:

<code>
#include <iostream>
#include <functional>

//------------------ Templating:

template< typename IntComparator >
bool foo( IntComparator const& compare, int const a, int const b )
{
return compare( a, b );
}

//------------------ Function pointer:

bool bar( bool compare( int, int ), int const a, int const b )
{
return compare( a, b );
}

bool isLess( int const a, int const b ) { return (a < b); }

//------------------ Runtime polymorphism:

struct Comparator
{
virtual ~Comparator() {}
virtual bool operator() ( int const a, int const b ) const = 0;
};

bool zed( Comparator const& compare, int const a, int const b )
{
return compare( a, b );
}

struct LessThan: Comparator
{
virtual bool operator() ( int const a, int const b ) const
{
return (a < b);
}
};

//------------------ Test:

int main()
{
using namespace std;
cout << boolalpha;

cout << "Using templating:" << endl;
cout << " (41 < 42) = " << foo( less<int>(), 41, 42 ) << endl;
cout << " (42 < 42) = " << foo( less<int>(), 42, 42 ) << endl;
cout << endl;

cout << "Using function pointer:" << endl;
cout << " (41 < 42) = " << bar( isLess, 41, 42 ) << endl;
cout << " (42 < 42) = " << bar( isLess, 42, 42 ) << endl;
cout << endl;

cout << "Using runtime polymorphism:" << endl;
cout << " (41 < 42) = " << zed( LessThan(), 41, 42 ) << endl;
cout << " (42 < 42) = " << zed( LessThan(), 42, 42 ) << endl;
cout << endl;
}
</code>

Boost har en del TMP-programmerings-st�tte der du kan skrive mer direkte ditt
eksempel, men jeg tror det er best � mestre teknikkene ovenfor f�rst...


Cheers & hdhd.,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!

Sigurd Stenersen

unread,
May 6, 2009, 2:33:11 PM5/6/09
to
"AB" <_@_.invalid> skrev

bool foo(char op, int a, int b)
{
switch (op)
{
case '<':
return a < b;
case '>':
return a > b;
...
}
throw something;
}

void main()
{
if (foo('<', 1, 2))
{
...
}
...
}


Sigurd

Dag-Erling Smørgrav

unread,
May 7, 2009, 7:30:43 AM5/7/09
to
AB <_@_.invalid> writes:
> Er det i det hele tatt mulig å angi en logis operator som argument til
> en funksjon på en eller annen måte (eller må jeg ty til Lisp)?

Bruk en funksjonspeker eller funktor e.l.

Forøvrig har både Perl og C# (og i mindre grad Python) lambda-uttrykk
eller tilsvarende mekanismer.

DES
--
Dag-Erling Smørgrav - d...@des.no

0 new messages