473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Diggins PDP #2 : Meta-Binders (binding functions to functions)

// meta_binder.hpp
// The Diggins PDP (Public Domain Post) #2
// Public Domain code by Christopher Diggins, May 22, 2005
//
// Description:
// A meta-binder binds function objects to function objects
//
// Motivation:
// lack of a logical_xor in the standard library, and no way to construct
one using the
// standard library using binders and adapters.

#ifndef META_BINDER_HPP
#define META_BINDER_HPP

#include <stdexcept>
#include <functional>

namespace cdiggins
{
template<class BinOp, class BinOpArg1, class BinOpArg2>
class meta_binder2 : public std::binary_function
<
typename BinOp::first_argument_type,
typename BinOp::second_argument_type,
typename BinOp::result_type

{
protected:
BinOp op;
BinOpArg1 arg1;
BinOpArg2 arg2;
public:
meta_binder2(BinOp x, BinOpArg1 a1, BinOpArg2 a2) : op(x), arg1(a1),
arg2(a2)
{ }
typename BinOp::result_type operator()
(const typename BinOp::first_argument_type& x,
const typename BinOp::second_argument_type& y)
{
return op(arg1(x, y), arg2(x, y));
}
};

template<class BinOp, class BinOpArg1, class BinOpArg2>
meta_binder2<BinOp, BinOpArg1, BinOpArg2>
meta_bind2(BinOp op, BinOpArg1 arg1, BinOpArg2 arg2) {
return meta_binder2<BinOp, BinOpArg1, BinOpArg2>(op, arg1, arg2);
}
}

void test(bool b) {
if (!b)
throw std::runtime_error("test failed");
}

namespace meta_binder_test
{
using namespace cdiggins;

bool logical_xor(bool x, bool y) {
return (x || y) && !(x && y);
}

void xor_test(bool x, bool y) {
test(logical_xor(x, y) ==
meta_bind2(
std::logical_and<bool>(),
std::logical_or<bool>(),
std::not2(std::logical_and<bool>())
)(x, y));
}

void test_main()
{
xor_test(true, true);
xor_test(true, false);
xor_test(false, true);
xor_test(false, false);
}
}

#endif

--
Christopher Diggins
http://www.cdiggins.com
Jul 23 '05 #1
1 1363
christopher diggins wrote:
// meta_binder.hpp
// The Diggins PDP (Public Domain Post) #2
// Public Domain code by Christopher Diggins, May 22, 2005
//
// Description:
// A meta-binder binds function objects to function objects
//
// Motivation:
// lack of a logical_xor in the standard library, and no way to construct one using the
// standard library using binders and adapters.


Did it ever occur to you that normal C++ programmers may be reluctant
to write something like:

logical_xor(x, y) ==
meta_bind2(
std::logical_and<bool>(),
std::logical_or<bool>(),
std::not2(std::logical_and<boo*l>())
)(x, y);
?

Jul 23 '05 #2

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

Similar topics

1
by: Cezary | last post by:
Hello. I was read PHP manual, but i'm not sure yet. Here is my meta tags in html: <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-2"> <META HTTP-EQUIV="Expires"...
4
by: Brian | last post by:
Hi, I'm trying to use standard meta tags in an xsl doc and using cocoon as my processor. The problem is that cocoon changes for example: <meta name="keywords" content="test, test, test" /> ...
19
by: Christian Hvid | last post by:
Hello groups. I have a series of applet computer games on my homepage: http://vredungmand.dk/games/erik-spillet/index.html http://vredungmand.dk/games/nohats/index.html...
24
by: Day Bird Loft | last post by:
Web Authoring | Meta-Tags The first thing to understand in regard to Meta Tags is the three most important tags placed in the head of your html documents. They are the title, description, and...
4
by: christopher diggins | last post by:
Welcome to the first installment of the Diggins Public Domain Posts (PDP). There is a significant dearth of good public domain C++ code. All of it seems to come with one kind of a license or...
0
by: christopher diggins | last post by:
// binary_aritmetic.hpp // Diggins PDP (Public Domain Post) #1.2 // by Christopher Diggins, May 24, 2005 // // Comment: // Provides several algorithms for unsigned binary arithmetic // //...
0
by: christopher diggins | last post by:
// The Diggins PDP (Public Domain Post) #4 // public domain code for computing the FFT // contributed by Christopher Diggins, 2005 template<int N> unsigned int bit_reverse(unsigned int x) {...
3
by: J1C | last post by:
How can I programatically add meta tags with javascript?
5
by: RodneyDunes | last post by:
My site did validate and now it doesn't. The error I get is the following: document type does not allow element "META" here ....nt-type" content="text/html;charset=iso-8859-1"> Can someone...
1
by: Maziar Aflatoun | last post by:
Hi everyone, My goal is to modify the contents of my meta tag (html refresh). However, my code adds a new instance of the meta tag at the bottom of the page. Is there a way to modify it instead...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.