473,508 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

look-up table for std::binary_function's

i don't manage to get this compiled:

// shall map compare operator ids to proper std::binary_function's
// for this instantiated for various types
template <typename ForType>
struct CompareOperator {
enum Id {equal = 0, less = 1 }; // predicate ids for lookup
typedef std::binary_function<ForType, ForType, boolfn_type;

static std::equal_to<ForTypem_equal_to;
static std::less<ForTypem_less;

static fn_type lookup[];
};

// local static instances of predicators
// (otherwise i have temporaries - right?)
template <typename ForType>
std::equal_to<ForTypeCompareOperator<ForType>::m_e qual_to;

template <typename ForType>
std::less<ForTypeCompareOperator<ForType>::m_less;

// look-up array for predicates
template <typename ForType>
typename CompareOperator<ForType>::fn_type
CompareOperator<ForType>::lookup[]
= { m_equal_to, m_less } ;
It fails for using - e.g. here:

// type dispatcher
// templated for the predicates
template <int ComparatorId>
inline bool CompareData::compare_operator_wrapper(CompareData const&
cd) const
{
switch (this->m_type) {
case MyTypeint32_c:
return
CompareOperator<MyTypeint32_t>::lookup[ComparatorId]
(m_data.int_32_val,
cd.m_data.int_32_val);
case MyTypeuint16_c:
return
CompareOperator<MyTypeuint16_c>::lookup[ComparatorId]
(m_data.uint_16_val,
cd.m_data.uint_16_val);

...
}
throw something;
}

// simplified implementation for 'CompareData' with dynamic type by
union
// and type if (coming from some C-API)

bool CompareData::operator<(const CompareData& cd) const
{
const int op = CompareOperator<void*>::less;
return compare_operator_wrapper<op(cd);
}
The purpose is to minimize LOC, because without this, every operator<|
==|<=|>= had implemented the type dispatcher.

But the compiler complains:
[CXXD] rule.cc
rule.cc: In member function `bool
EventScheduler::CompareData::compare_operator_wrap per(const
EventScheduler::CompareData&) const':
rule.cc:422: error: no match for call to
`(std::binary_function<IDBMint32_t, IDBMint32_t, bool>) (IDBMint32_t&,
IDBMint32_t&)'
rule.cc:424: error: no match for call to
`(std::binary_function<IDBMuint16_t, IDBMuint16_t, bool>) (const
IDBMuint16_t&, const IDBMuint16_t&)'
rule.cc:426: error: no match for call to
`(std::binary_function<IDBMreal64_t, IDBMreal64_t, bool>) (const
IDBMreal64_t&, const IDBMreal64_t&)'

....

I understand, that std::binary_function<...itself doesn't have the
operator(..) to invoke.
But what's the correct way to create such predicate look-up table
then?

- many thanks in advance!

rgds
Frank
Jun 27 '08 #1
1 2488
boost::function2<...is my friend :-)

But i wonder about following:

template<typename ForType>
struct CompareOperators {
enum Id { equal_to = 0, not_equal_to, less, less_equal, greater,
greater_equal };
typedef boost::function2<bool, ForType, ForTypefn_t;
static fn_t lookup[];
}

template<typename ForType>
typename CompareOperators<ForType>::fn_t
CompareOperators<ForType>::lookup[]
= {
std::equal_to<ForType>(),
std::not_equal_to<ForType>(),
std::less<ForType>(),
std::less_equal<ForType>(),
std::greater<ForType>(),
std::greater_equal<ForType>()
};
The initialization of CompareOperators<ForType>::lookup[] requires to
use "std::equal_to<ForType>()", etc.
It does not accept "std::equal_to<ForType>", etc.
Is this not a temporary for initialization, which is going out of scope?
Or does boost::function2<...bind to that and "hold it"?
Or does it not matter anyways, because std::equal_to<is stateless and
only contains operator() member?

rgds

Frank
Jun 27 '08 #2

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

Similar topics

2
2305
by: Maitre Bart | last post by:
First, I describe my setup in 3 points. Then I describe what I want to do, and describe the error I get. 1) I have a container GTTable container pointer to data (class GT *). 2) I made a...
8
1496
by: Hunter Hou | last post by:
hello, I'm reading <the C++ programming language>, here's a question for bind2nd. Who can help explain what on earth bind2nd is working? .... void rotate_all( list<Shape*>& ls, int angle ) {...
13
1613
by: jsnX | last post by:
say i have a function object silly that takes a const ref to clowns class silly : public std::unary_function<const clown&, bool> { ... } and then i decide to feed it a bunch of pointers to...
5
2983
by: dave_if | last post by:
I have a vector of TCard objects, I am sorting them based on the the box field. That part works fine (finally!). I would then like to random_shuffle all cards that are in a particular box, that is,...
5
1528
by: vulpes | last post by:
This similar situation will explain somewhat, what I want to do: class X { public: X() : map_(*this) {} bool operator() (int a, int b) { return goodStuff(a) < goodStuff(b); } int...
0
7226
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
7125
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
7328
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,...
1
7049
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...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
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...

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.