Friday, July 21, 2006

C Pun Contest

Some of C puns i really enjoyed.

Q: What do you get when you assign (char) 0 to Arnold Schwarzenegger?
A: A Null Terminator.


#ifdef Get_A_Hearing_Aid


Did you hear about the C+ + programmer whose girlfriend broke up with him because he treated her like an object?


Source: C/C++ User Journal May 1992

Thursday, July 20, 2006

Heap checking class

I found the idea of heap checking class in C User Journal of April 1991. The class is really useful and the following is my own implementation of heap checking class.

class HeapCheck
{
char* begin;
char* end;

public:
HeapCheck()
{
begin = new char;
delete begin;
}

bool IsOk()
{
bool ok;

end = new char;
ok = (end == begin);
delete end;

return ok;
}
}
;

Possible implementation of va_list


I found this in C/C++ Users Journal Jan 1990

typedef int * va_list;

#define va_start (arg_ptr, first_param) (arg_ptr= (va_list) (&first_param+1))
#define va_arg(arg_ptr,type) (* (((type*) (arg_ptr= (va_list) ((type*) arg_ptr) +1))-1))
#define va_end(arg_ptr) \
(arg_ptr=NULL)

Wednesday, July 19, 2006

The History Behind the Keyword 'typename'

Today when i am reading the Game Institute CPP Programming Module 2 i come across this keyword 'typename'. I never heard this C++ keyword before so i googled and fount this fact from http://blogs.msdn.com/slippman/archive/2004/08/11/212768.aspx

Why C++ Supports both Class and Typename for Type Parameters

Recently, someone asked me why we support both class and typename within C++ to indicate a type parameter since the keywords do not hold any platform significance – for example, class is not meant to suggest a native type nor is typename meant to suggest a CLI type. Rather, both equivalently indicate that the name following represents a parameterized type placeholder that will be replaced by a user-specfied actual type.

The reason for the two keywords is historical. In the original template specification, Stroustrup reused the existing class keyword to specify a type parameter rather than introduce a new keyword that might of course break existing programs. It wasn't that a new keyword wasn't considered -- just that it wasn't considered necessary given its potential disruption. And up until the ISO-C++ standard, this was the only way to declare a type parameter.

Reuses of existing keywords seems to always sow confusion. What we found is that beginners were whether the use of the class constrained or limited the type arguments a user could specify to be class types rather than, say, a built-in or pointer type. So, there was some feeling that not having introduced a new keyword was a mistake.

During standardization, certain constructs were discovered within a template definition that resolved to expressions although they were meant to indicate declarations. For example,

template

class Demonstration {

public:

void method() {

T::A *aObj; // oops …

// …

};

While the statement containing aObj is intended by the programmer to be interpreted as the declaration of a pointer to a nested type A within the type parameter T, the language grammar interprets it as an arithmetic expression multiplying the static member A of type T with aObj and throwing away the result. Isn't that annoying! (This sort of dilemna is not possible within generics – there is no way to safely verify that any T contains an A so that the runtime can safely construct an instance of the generic type.)

The committee decided that a new keyword was just the ticket to get the compiler off its unfortunate obsession with expressions. The new keyword was the self-describing typename. When applied to a statement, such as,

typename T::A* a6; // declare pointer to T’s A

it instructs the compiler to treat the subsequent statement as a declaration. Since the keyword was on the payroll, heck, why not fix the confusion caused by the original decision to reuse the class keyword. Of course, given the extensive body of existing code and books and articles and talks and postings using the class keyword, they chose to also retain support for that use of the keyword as well. So that's why you have both.

Published Wednesday, August 11, 2004 9:32 AM by slippman

Also check this comment

Michael Kleber said:
I just had to use typename for the "unfortunate obsession with expressions" sense and I still don't understand why. Consider the following moronic way to find the length of a vector:

using namespace std;
template
int vcount(vector v) {
int i;
typename vector
::iterator vi;
for(i=0,vi=v.begin(); vi!=v.end(); i++,vi++)
;
return(i);
}

Much to my surprise, g++ 3.4.0 didn't like vector
::iterator until I put a typename before it. (Looks like older g++'s think it's just fine.)

I remain baffled, but at least typename was there for me!

I checked the above code with gcc and its true