c++ - How to workaround warning C4333 ('>>' : right shift by too large amount, data loss) -


i have following function convert integer of arbitrary size buffer:

template<typename t> std::string build_data_from(t val) {   std::string result;    (int = 0; < sizeof(val); i++)   {     result.insert(0, 1, char(val));     val = val >> 8;   }    return result; }; 

however, invoking template function unsigned char renders warning in visual c++ 2008:

std::string x(build_data_from<unsigned char>(1)); 

warning c4333: '>>' : right shift large amount, data loss

is there clean way (without using pragma warning directive) workaround it?

pretty simple: overloading build_data_from unsigned char (and char).

this can done either plain overload or using std::enable_if, i'd advise plain overload it'll easier:

std::string build_data_from(char val) {   std::string result; result += val; return result; }  std::string build_data_from(unsigned char val) {   return build_data_from(char(val)); } 

but, conscious casting unsigned char char might produce weird output right ? (i mean unsigned char might have values not printable)


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -