[Update] boost::decay documentation issue
Posted on January 14th, 2010 at 17:35 by fr3@K
What Happens When We Lie to the Compiler?
Posted on November 13th, 2009 at 12:57 by fr3@K
When you lie to your compiler, don’t be surprised to get results those differ from what you expect.
In this case, the coder promised his/her compiler the value of “value” won’t be changed, as it was declared const.
Therefore our best friend (yes, the compiler) who trusted the coder, thought “hey, I could cache this value in a register and not fetch it from RAM every time I reference it”, i.e. optimization.
If we change the code in the link slightly, so that function funcC would print *value before the assignment, one would see that the value of *value had actually been modified by function funcA. However, the modification was not visible in main, because the compiler was lied to as the coder explicitly told it that there would be no modification to value.
In the body of function funcC, though value was declared as a pointer to const instance, the compiler was smart enough to know it was lied to. There was a const_cast and a assignment, therefore, the modification to *value was reflected in the output.
Boost.Lexical-Cast Alternatives
Posted on September 17th, 2009 at 21:49 by fr3@K
今天稍早, 跟一位同事聊到 Boost 的 lexical_cast. 打開了 Firefox 請出 Google, 想找到能幫助我介紹 lexical_cast 的資源. 意外地, Google 把我帶到讓我賺了介紹獎金, 現在跟我是同事的 Jeff, 談到 lexical_cast 的 文章.
在這篇文字的討論串, Jeff 寫道:
我覺得如果可以用這樣的寫法,那就太好了:
uint32_t ID = lexical_cast<auto>("1234567");
甚至最好是:
template <class From, class To = auto>
To lexical_cast(From s) {...}
uint32_t ID = lexical_cast("1234567");
Jeff Hung
(more…)