C++类型转换(无精度降低)

#include <cassert>
/**
* Cast one size of int down to another one.
* Asserts that no precision is lost at runtime.
*/
template<typename Small, typename Large>
inline Small
downCast(const Large& large)
{
Small small = static_cast<Small>(large);
// The following comparison (rather than "large==small") allows
// this method to convert between signed and unsigned values.
assert(large-small == 0);
return small;
}
 
节选自 https://github.com/PlatformLab/NanoLog.git
中 "common.h" 文件.
posted @ 2019-12-28 10:21  hbg-rohens  阅读(382)  评论(0编辑  收藏  举报