mardi 14 avril 2015

Why std::is_same does not work?

My code snippet is:



namespace serialization
{
struct output_stream
{
/////
void write(const void* data, size_t size)
{
const byte_t* p = static_cast<const byte_t*>(data);

size_t old_size = buffer_.size();
buffer_.resize(old_size + size);
memcpy(buffer_.data() + old_size, p, size);
}
}

struct input_stream
{
///////

void read(void* to, size_t size)
{
assert(cur_ + size <= end_);
memcpy(to, cur_, size);
cur_ += size;
}
}
}

template <class stream_t>
void serialize(not_pod_struct& r, stream_t stream)
{
if (std::is_same<stream_t, serialization::output_stream>::value)
{
stream.write(&r, sizeof(r));
}
else if (std::is_same<stream_t, serialization::input_stream>::value)
{
not_pod_struct* buf = new not_pod_struct[sizeof(not_pod_struct)];
stream.read(buf, sizeof(not_pod_struct));
r = *buf;
}
else
{
throw std::invalid_argument("stream_t is not a stream.");
}
}

template<class T>
typename enable_if<!is_pod<T>::value, void>::type
read(input_stream & input_stream, T & non_pod_struct)
{
serialize(non_pod_struct, input_stream);
}

template<class T>
typename enable_if<!is_pod<T>::value, void>::type
write(output_stream & output_stream, T & non_pod_struct)
{
serialize(non_pod_struct, output_stream);
}


I have errors:



Error 1 error C2039: 'read' : is not a member of 'serialization::output_stream'
Error 2 error C2039: 'write' : is not a member of 'serialization::input_stream'


This is strange. I do not understand why these errors happen.


Aucun commentaire:

Enregistrer un commentaire