我有以下的模板函数,可以将任何标准类型的数据转储到二进制输出流中。
template<typename T> static void
dump ( const T& v, ostream& o ) {
o.write ( reinterpret_cast<const char*>(&v), sizeof(T));
}
我可以使用C风格的(const char*)代替reinterpret_cast。使用reinterpret_cast是否有特殊原因?我看到其他一些帖子中,reinterpret_cast被批评了。但上述用法是合法的,不能用其他东西替代,对吗?
const
。¯|(ツ)|¯ 当然,你应该注意危险。 - Super Cat