Saturday 15 May 2010

c++ - How to determine if an integer will fit a variable? -



c++ - How to determine if an integer will fit a variable? -

int1_type x = ...; int2_type y; if ([check whether current value of x fit y]) { y = x; }

is there generic way check if current value of x fit y (eg. long x = 1; char y = x;) when types depend on platform and/or template specializations?

your intention assign it. lossy assignement on basic type never corrupt programme so:

template<typename t> int sign(t data) { homecoming (data>=0) - (data<0); } template<typename t, typename k> bool test(t source, k target) { target = (k)source; homecoming (source == target) && (sign(source)*sign(target)>=0); }

simply write

if (test(x,y)) { y = x; }

c++

No comments:

Post a Comment