Why following code snippet compile failed on Mac platform using clang++?
The sizeof
unsigned long
and uint64_t
is both 8, so I think they are the same type. So why the compile think the Serializer<unsigned long>
is abstract?
Because I have define Serializer<uint64_t>, Serializer<int64_t>, Serializer<uint32_t>, Serializer<int32_t>, Serializer<uint16_t>, Serializer<int16_t>, Serializer<uint8_t>, Serializer<int8_t>
, is there any way to resolve this issue and avoid define more type like Serializer<unsigned long>
?
Following is the error message, both clang++ and g++ give the similar result:
example.cpp:23:31: error: variable type 'Serializer' is an abstract class
Serializer<unsigned long> s; ^ example.cpp:6:25: note: unimplemented
pure virtual method 'ToString' in 'Serializer'
virtual std::string ToString(const T* val) = 0; ^ 1 error generated.
#include <iostream>
#include <vector>
#include <algorithm>
template <typename T>
class Serializer {
virtual std::string ToString(const T* val) = 0;
};
template <>
class Serializer<uint64_t> {
public:
virtual std::string ToString(const int8_t* val) {
return "";
}
};
int main(int argc, const char *argv[])
{
// both of the size is 8 bytes
std::cout << " size of unsigned long:" << sizeof(unsigned long) << " sizeof uint64_t:" << sizeof(uint64_t);
// following compile error happen
Serializer<unsigned long> s; //<------- Error happen here
s.ToString(NULL);
return 0;
}