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" 文件.

猜你喜欢

转载自www.cnblogs.com/rohens-hbg/p/12110896.html