Posts Tagged ‘C++’

std::to_string fixed in Visual Studio 2012

July 13, 2012 Leave a comment

Among many other fixes coming in the new version of Visual Studio, is a fix to std::to_string (introduced in Visual Studio 2010) providing the full range of numerical types. Currently using the function with any numerical type other than long long or long double or unsigned long long yields compiler error C2668 – ambiguous call to overloaded function.

Whilst annoying, there’s a trivial workaround for Visual Studio 2010 to still use this handy function without resorting to boost::lexical_cast by static_casting to one of the three currently provided overloads. As of VS 2012, such massaging of types is no longer required. 🙂

Categories: Programming Tags: , ,

Silent promotions

May 13, 2012 Leave a comment

A few days ago I came across a bit of nasty behaviour when evaluating an expression performing a simple calculation on a signed and unsigned integer. Take the following C++ code snippet:

unsigned int nA = 10;
int nB = 11;

if( nA – nB < 0 )
// etc…

On first inspection, given the inputs I would expect the expression to evaluate to true. What surprised me is that, despite no compiler warnings being emitted using Visual Studio 2010 at warning level 4, the statement evaluates to false.

It turns out the signed int is silently promoted to an unsigned int which changes things. The answer to 10 – 11 is no longer -1 but 4294967294 ( or UINT_MAX-1 ) since an unsigned integer can’t hold a negative number (no ‘spare’, top bit for sign indication) and has wrapped around.

There are a few different fixes depending on the what your function is trying to do. Assuming mixing signed and unsigned types is unavoidable, the following fix to this is simple assuming there can be no range violations and you won’t end up with a large negative number for the left hand side:

if( static_cast( nA ) – nB < 0 )

I understand that gcc does emit a warning about this with -Wextra but the usual advice of test test test is hard to beat. Did I mention that I’m a huge fan of unit tests?

Categories: Programming Tags: ,

Gasp… an update?

February 7, 2010 1 comment

Hello Interwebiverse!

Yes I am still around (as some of you who are following my meanderings on twitter are aware) and am still interested in my blog. Finding free time at the moment is a huge issue for me (just one look at the stack of games in my ‘to-do’ pile speaks to that) but I hope to use my blog as something akin to a more verbatim twitter feed. Twitter has been much easier to post to given the (forced) laconicity of the medium and I have been tweeting fairly regularly for the last few years. Naturally there have been times when I would have wanted to expand what I was tweeting about into a more concise discussion allowing it to coalesce outside of the 160 character limit.

I have formerly made no bones about my aversion to micro blogging (or link blogging for that matter) and this is not where I am leaning. To be honest, I am not entirely sure exactly how this new setup will come together yet, I am looking at the possibility of moving away from a hosted wordpress account to doing something self hosted. Whilst that would give me the opportunity to develop my own (or tweak an existing) CMS system, the problem of maintenance rears its ugly head. Security on the web is paramount the issue of platform is a double-edged sword. Developing a proprietary solution (fun, although likely very time consuming) would prevent off the shelf attacks against known exploits although I do not have enough experience in PHP/MySQL yet to be sufficiently confident in my hardening skills. On the other side of the spectrum, using an off the shelf solution like wordpress would give me the instant bonus of a CMS that I could customise, although I would have to be strict about keeping up with patches and security bulletins for the platform (highlighted by recent history).

So, as you can see, not only am I unsure where / how I am going to take this, I am also unsure of the platform I am going to use. A great start :-).

My interests have evolved and grown a lot in the last couple of years. It goes without saying (as it is my job) but I am very into coding and software development, I regularly write code in C++ and python outside of work now. This presents something else that I need to consider, I would love to write posts about some of this code and make it available for free on here, but I need to figure out how it all fits with the existing content on my blog.

But anyway, before I start rambling, I will be shortly looking into tweaking and starting up writing again. Please bear with me whilst I get everything together. 🙂

Categories: News Tags: , , , , ,
%d bloggers like this: