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: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
// 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: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