look-up table for std::binary_function's

F

Frank Bergemann

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, bool> fn_type;

static std::equal_to<ForType> m_equal_to;
static std::less<ForType> m_less;

static fn_type lookup[];
};

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

template <typename ForType>
std::less<ForType> CompareOperator<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::eek:perator<(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_wrapper(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
 
F

Frank Bergemann

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, ForType> fn_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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top