Hey. I want to write a helper function `count` to give me a int back, basing on the template parameter enum, which tell which int(8, 16, 32, 64) the pointer is pointing to and how to compare the predicate with the stored data. I am not sure about how to write such a template helper function with enum parameter, which I think not belong to a class.
I got some bug with having a template function with enum parameter.
(^^ is the postion the error comes)
--------for DataBlock.hh--------------------------
error: use of ‘this’ in a constant expression
return count<encodingType^^, predicate.compareType>(predicate.val, getDataLength(predicate.col_id), basePointer);
error: no matching function for call to ‘count<((DataBlock*)this)->DataBlock::encoding_type.std::array<imlab::EncodingType, 3>::operator[](((std::array<:EncodingType, 100>::size_type)predicate.Predicate::col_id)), predicate.Predicate::compareType>(uint64_t&, uint32_t, unsigned char*&)’
return count<encoding_types[predicate.col_id], predicate.compareType>^^(predicate.val, getDataLength(predicate.col_id), basePointer);
--------for util.hh--------------------------
note: candidate: template<EncodingType E, CompareType C> uint64_t count(uint64_t, uint32_t, uint8_t*)
uint64_t count(uint64_t val, uint32_t dataLength, uint8_t* basePointer);
^^~~~~
note: template argument deduction/substitution failed:
error: ‘this’ is not a constant expression
count<encoding_types[predicate.col_id], predicate.compareType>^^(predicate.val, getDataLength(predicate.col_id), basePointer);
note: in template argument for type ‘EncodingType’
error: ‘predicate’ is not a constant expression
note: in template argument for type ‘CompareType’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
//----------base enum and struct---------------
enum EncodingType {
byte1,
byte2,
byte4,
byte8
};
enum CompareType {
EQUAL,
GREATER_THAN,
LESS_THAN
};
struct Predicate {
uint32_t col_id;
uint64_t val;
CompareType compareType;
};
//---------------------util.hh----------------------------
//---------template function with enum-----------
template <EncodingType E, CompareType C>
uint64_t count(uint64_t val, uint32_t dataLength, uint8_t* basePointer);
template <>
uint64_t count<EncodingType::byte1, CompareType::EQUAL>(uint64_t val, uint32_t dataLength, uint8_t* basePointer) {
uint64_t counter = 0;
val = (uint8_t) val;
for(size_t in = 0 ; in < dataLength; in++) {
if (*(basePointer + in) == val) {
++counter;
}
}
return counter;
}
// other combinations are simliar
//------------DataBlock.hh--------------
class DataBlock{
// some attributs
std::array<EncodingType, 100> encoding_types;
// a member funciton in DataBlock class
uint64_t scan(Predicate& predicate) {
return count<encoding_types[predicate.col_id], predicate.compareType>(predicate.val, getDataLength(predicate.col_id), basePointer);
// assumed here we get the length via getDataLength(predicate.col_id) and the basePointer is correct
}
}
|