Parse v. TryParse

Parse throws an exception if it cannot parse the value, whereas TryParse returns a bool indicating whether it succeeded. TryParse does not just try/catch internally – the whole point of it is that it is implemented without exceptions so that it is fast. In fact the way it is most likely implemented is that internally … Read more

how to convert from int to char*?

In C++17, use std::to_chars as: std::array<char, 10> str; std::to_chars(str.data(), str.data() + str.size(), 42); In C++11, use std::to_string as: std::string s = std::to_string(number); char const *pchar = s.c_str(); //use char const* as target type And in C++03, what you’re doing is just fine, except use const as: char const* pchar = temp_str.c_str(); //dont use cast

Why is an int in OCaml only 31 bits?

This is called a tagged pointer representation, and is a pretty common optimization trick used in many different interpreters, VMs and runtime systems for decades. Pretty much every Lisp implementation uses them, many Smalltalk VMs, many Ruby interpreters, and so on. Usually, in those languages, you always pass around pointers to objects. An object itself … Read more

C++ performance challenge: integer to std::string conversion

#include <string> const char digit_pairs[201] = { “00010203040506070809” “10111213141516171819” “20212223242526272829” “30313233343536373839” “40414243444546474849” “50515253545556575859” “60616263646566676869” “70717273747576777879” “80818283848586878889” “90919293949596979899” }; std::string& itostr(int n, std::string& s) { if(n==0) { s=”0″; return s; } int sign = -(n<0); unsigned int val = (n^sign)-sign; int size; if(val>=10000) { if(val>=10000000) { if(val>=1000000000) size=10; else if(val>=100000000) size=9; else size=8; } else { … Read more

Why (0-6) is -6 = False? [duplicate]

All integers from -5 to 256 inclusive are cached as global objects sharing the same address with CPython, thus the is test passes. This artifact is explained in detail in http://www.laurentluce.com/posts/python-integer-objects-implementation/, and we could check the current source code in http://hg.python.org/cpython/file/tip/Objects/longobject.c. A specific structure is used to refer small integers and share them so access … Read more

Why isn’t `int pow(int base, int exponent)` in the standard C++ libraries?

As of C++11, special cases were added to the suite of power functions (and others). C++11 [c.math] /11 states, after listing all the float/double/long double overloads (my emphasis, and paraphrased): Moreover, there shall be additional overloads sufficient to ensure that, if any argument corresponding to a double parameter has type double or an integer type, … Read more

What is the default value of the nullable type “int?” (including question mark)?

The default value for int? — and for any nullable type that uses the “type?” declaration — is null. Why this is the case: int? is syntactic sugar for the type Nullable<T> (where T is int), a struct. (reference) The Nullable<T> type has a bool HasValue member, which when false, makes the Nullable<T> instance “act … Read more