Archive for September, 2009

How many bits in a byte?

September 29, 2009

Seems a simple enough questions doesn’t it?
Eight of course!
Well not if you are talking about a C++ byte as it is not defined how many bits there are in a C++ byte, this is why the header limits has CHAR_BIT.

Union trick to typecast.

September 28, 2009

Ok you have all seen it and maybe even used it, but it is Undefined Bahaviour as the standard forbids it. What I am talking about is the following piece of code:



union foo{ int a; float b;};

foo f;

f.a = 10;

float f = f.b;

You may even have seen people like Danny Kalev from InformIT, who was also a previous standard committee member, using it. I do not care he is wrong and here is why:

In a union, at most one of the data members can be active at any time, that is, the value of at most one of the data members can be stored in a union at any time. [Note: one special guarantee is made in order to simplify the use of unions: If a POD-union contains several POD-structs that share a common initial sequence (9.2), and if an object of this POD-union type contains one of the POD-structs, it is permitted to inspect the common initial sequence of any of POD-struct members; see 9.2.

Please do not comment about this working in implementation X using compiler Y or on platform Z, it is none standard C++.

Twitter account retired

September 23, 2009

Today I retired the twitter account because it was becoming difficult to compress what I wanted to post in 140 chars. This lead to tweets which were edited and re-edited so as not to bombard people with updates which I feel was politest method, as otherwise people would become disinterested and ignore messages. This is one downside to twitter.

Pre-empted FAQS

Q. Why did you delete the account and not instead just stop posting to it?

A. I think is was the correct action rather than have dormant accounts being followed and requiring followers to unfollow it.

Q. So does this mean you will be posting to the blog more?

A. Hmm well I am not very good at updating blogs as they require more thought and time to write posts, this was one of the good points about twitter. For example my personal blog has not been updated in about twelve months. I can only say I will attempt to update this one.

Rarely implemented part of C++

September 19, 2009

This post is to show the relevant part of the standard which relates to the following:

This is a little known and a rarely implement part of C++. A constant integral class member type can be initialised in-line yet according to the standard it also requires a definition without an initial value if it is used.

9.4.2.4
If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer.

Copy constructor not in public interface.

September 17, 2009

This is something I tweeted about yet I felt it required a snippet to show what I meant, so here it is.


class foo
{
public:
	foo(int i):bar(i){}
	int bar;
private:
	foo(foo const&);//prevent copy constructor
};

int baz(foo const& f)
{
	return f.bar;
}
int main()
{
	return baz( foo(0) );
}

This will fail to compile when no language extensions are turned on; yet by removing the “private” keyword and therefore taking the unimplemented copy constructor into the public interface it will compile.

How not to learn C++

September 15, 2009

When a new user to C++ posts generally in a Linux forum/mailing list about how to learn C++ the following is a response which you will find.

If it were me, I’d pick some C++ open source tool I actually use and which I think is lacking some feature I care about. Then I’d get the source code and try to figure out where/how the related features are implemented, then add the feature I want.

So what is wrong with a large majority of Open Source projects.

  • Some use API specific calls which are not part of the C++ or C standard for example POSIX or WIN32.
  • Some use Undefined Behaviour without even knowing it.
  • Some use poor programming practices, such that they are like a tangled web.

OK so what if the Open Source project does not include any of the above?

I feel it is still incorrect to suggest for a new comer that they should delve into a large project. Unless the project is well documented (I do not mean in line comments) or Test Driven Developed ( TDD) which shows correct usage of the project, then you are asking a novice to the Language to find it out for themselves! Theses types of  projects are not designed with the core principle of being newbie friendly.  So please let us be considerate here, if we take this into the real world would you say to a school leaver?

The best way how to learn how to build a skyscraper is to look at that one over there and make some modifications.

or

Go and look at the electrics of that house and and add a spare to that circuit

Now you could learn something from a project like this, yet the downfalls far outweigh the benefits. These new comers could be building on very rocky foundations and learning how to design or implement wrongly from the outset. I think it is akin to a new comer posting a tutorial on line which another new comer will read and try to implement.

Coding is a profession so lets treat it like such, learn from books*  which are written by people with real knowledge and you will build on solid foundations.

*Disclaimer there are some really bad books. Cough