为什么 std::is_same 不起作用
Why std::is_same does not work?
我的代码片段是:
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);
}
我有错误:
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'
这很奇怪。我不明白为什么会发生这些错误。
if
在某些其他语言中没有static if
。未到达的分支仍必须编译。
标签调度(如果你正在做比is_same
更复杂的事情(例如,根据is_input_stream
特征接受各种流);使用只接受两种类型并用每种类型做完全不同的事情的模板没有多大意义):
template <class stream_t>
void serialize(not_pod_struct& r, stream_t & stream,
std::true_type /* is_input */, std::false_type /* is_output */)
{
not_pod_struct* buf = new not_pod_struct[sizeof(not_pod_struct)];
stream.read(buf, sizeof(not_pod_struct));
r = *buf;
}
template <class stream_t>
void serialize(not_pod_struct& r, stream_t & stream,
std::false_type /* is_input */, std::true_type /* is_output */)
{
stream.write(&r, sizeof(r));
}
template <class stream_t>
void serialize(not_pod_struct& r, stream_t & stream)
{
serialize(r, stream, std::is_same<stream_t, serialization::input_stream>(),
std::is_same<stream_t, serialization::output_stream>());
}
或者只是超载serialize
output_stream
和input_stream
:
void serialize(not_pod_struct& r, serialization::input_stream & stream)
{
not_pod_struct* buf = new not_pod_struct[sizeof(not_pod_struct)];
stream.read(buf, sizeof(not_pod_struct));
r = *buf;
}
void serialize(not_pod_struct& r, serialization::output_stream & stream)
{
stream.write(&r, sizeof(r));
}
我冒昧地让serialize
引用接受流。另外,我认为您的"阅读"逻辑不正确。你不仅泄漏了分配new
的内存,我非常怀疑你的意思是分配一个sizeof(not_pod_struct)
not_pod_struct
数组。
此代码
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;
}
没有按照你的想法做。实例化函数模板serialize
时,其代码必须格式正确,但事实并非如此。考虑内部发生了什么,例如serialize<output_stream>
:
if (true)
{
stream.write(&r, sizeof(r));
}
else if (false)
{
// This branch never executes, but this doesn't mean it's not compiled!
not_pod_struct* buf = new not_pod_struct[sizeof(not_pod_struct)];
// And output_stream doesn't have read() method.
stream.read(buf, sizeof(not_pod_struct));
r = *buf;
}
您还可以使用完整的模板规范,并在使用input_stream或output_stream以外的其他参数调用序列化时引发异常。这接近您的原始代码。
template<typename X>
void serialize(not_pod_struct & r, X & x)
{
throw std::invalid_argument("stream_t is not a stream.");
}
template<>
void serialize(not_pod_struct& r, serialization::input_stream & stream)
{
not_pod_struct* buf = new not_pod_struct[sizeof(not_pod_struct)];
stream.read(buf, sizeof(not_pod_struct));
r = *buf;
}
template<>
void serialize(not_pod_struct& r, serialization::output_stream & stream)
{
stream.write(&r, sizeof(r));
}
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用