Why doesn’t a derived template class have access to a base template class’ identifiers?

That’s two-phase lookup for you. Base<T>::NO_ZEROFILL (all caps identifiers are boo, except for macros, BTW) is an identifier that depends on T. Since, when the compiler first parses the template, there’s no actual type substituted for T yet, the compiler doesn’t “know” what Base<T> is. So it cannot know any identifiers you assume to be … Read more

Different floating point result with optimization enabled – compiler bug?

Intel x86 processors use 80-bit extended precision internally, whereas double is normally 64-bit wide. Different optimization levels affect how often floating point values from CPU get saved into memory and thus rounded from 80-bit precision to 64-bit precision. Use the -ffloat-store gcc option to get the same floating point results with different optimization levels. Alternatively, … Read more

Convert string to int with bool/fail in C++

Use boost::lexical_cast. If the cast cannot be done, it will throw an exception. #include <boost/lexical_cast.hpp> #include <iostream> #include <string> int main(void) { std::string s; std::cin >> s; try { int i = boost::lexical_cast<int>(s); /* … */ } catch(…) { /* … */ } } Without boost: #include <iostream> #include <sstream> #include <string> int main(void) { … Read more

How to get IOStream to perform better?

Here is what I have gathered so far: Buffering: If by default the buffer is very small, increasing the buffer size can definitely improve the performance: it reduces the number of HDD hits it reduces the number of system calls Buffer can be set by accessing the underlying streambuf implementation. char Buffer[N]; std::ifstream file(“file.txt”); file.rdbuf()->pubsetbuf(Buffer, … Read more

Copy constructor and = operator overload in C++: is a common function possible?

Yes. There are two common options. One – which is generally discouraged – is to call the operator= from the copy constructor explicitly: MyClass(const MyClass& other) { operator=(other); } However, providing a good operator= is a challenge when it comes to dealing with the old state and issues arising from self assignment. Also, all members … Read more

Why do I see strange values when I print uninitialized variables?

Put simply, var is not initialized and reading an uninitialized variable leads to undefined behavior. So don’t do it. The moment you do, your program is no longer guaranteed to do anything you say. Formally, “reading” a value means performing an lvalue-to-rvalue conversion on it. And ยง4.1 states “…if the object is uninitialized, a program … Read more