C++ Study Note (3) – typename, class and template

Development February 27th, 2007

C++ is strong type-safe language, the C++ compiler needs to check type and verify syntax correctness. C++ reuse the “*” as pointer dereference, also multiple, the template reuse the “< >“, which might be used in the stream “<<, >>” or logic operation, ( less than, great than ). C++ compiler may not figure out the ambiguous syntax meaning in some cases, for example:

template< class T, typename U> foo;

foo< int, vector<int>> v;  

// The following examples are copied from text book B1.1

iterator_traits<FwdIterator1>::value_type* pi = &*i;    

template <typename T, typename T::value_type>  struct sqrt_impl;  

{  return  x.convert<3>(pi);  }

Ln 1 demostrates either class or typename can be used to declare the template parameter.

Ln 2 shows a pitfall for “> >”, if there is no space between two >, the compiler would regards it as “>>” stream operator instead.

Ln 4 is a typical case when to use typename. The compiler could not determine whether

iterator_traits::value_type

is a type or a value. If it is a type, * would be the pointer decorator, otherwise, * is the multiplex. We need to help the compiler to clarify the ambiguousness by adding typename decorator like this:

typename iterator_traits::value_type

Ln 5 is the example of misusing typename as the template parameter. Since typename T::value_type could also regard as the typename declaration. The best approach is to use class only.

Ln 6 is the example when to use template keyword to disambiguate template. x.convert could be a template member function or member variable; therefore, the succeeding < > could interpreted as template or less than, great than operators. The work-around is like this:

{  return  x.template convert<3>(pi);  }

Here are some rules of thumbs:

  • typename is required anywhere in templates on qualified dependent names that denote type.
  • typename is forbidden on the name of base class.
  • template is required before dependent names accessing member templates via . , ->, or :: qualification.

C++ Study Note(1): Size matters

Development February 17th, 2007

C++ is so versatile that most likely the application runs with design flaws. Before we dive into this wonderful language, let me summarize overlooked tricks and pitfalls for you, all test cases are compiled and run in gcc (GCC) 4.1.1 (Gentoo 4.1.1-r3)

These are rules-of thumb for the object size:

  • The size of the empty object is minimum 1 instead of 0.
  • Static member does not consume space.
  • Objects with virtual function would pay the price for vtable, aka 4 bytes[1], the good news is this is fixed price.
  • Size of derived object is the sum of base objects.
#include <iostream>
using namespace std;

class Base {};

class StaticBase
{
public:
        static int foo;
};

class BaseWithVTable
{
public:
        virtual ~BaseWithVTable() {};
        virtual void bar() {};
};

class Derived : public BaseWithVTable
{
public:
        virtual ~Derived() {};
        void foo() {};
        int bar;
};

class MoreDerived: public Derived , public StaticBase
{
private:
        int hello;
};

int main()
{
        cout << "sizeof(Base) = " << sizeof(Base) << endl;
        cout << "sizeof(StaticBase) = " << sizeof(StaticBase) << endl;
        cout << "sizeof(BaseWithVTable) = " << sizeof(BaseWithVTable) << endl;
        cout << "sizeof(Derived) = " << sizeof(Derived)  << endl;
        cout << "sizeof(MoreDerived) = " << sizeof(MoreDerived)  << endl;
}

The output is:

sizeof(Base) = 1
sizeof(StaticBase) = 1
sizeof(BaseWithVTable) = 4
sizeof(Derived) = 8
sizeof(MoreDerived) = 12

Make it work

Desktop, Gentoo, Palm February 12th, 2007

The KPilot 3.5.5 once worked fine for my Tungsten T, and it still served for the Treo application installation and backup. Unfortunately, the KPilot could not synchronize the TODO or calendar, the kpilotDaemonn just crashed without a trace, in fact that is my fault, the konqi is not installed in my system.

KPilot developers have released 3.5.6, and claimed that it has solved lots of long-lasting bug, including my patch as well. However, the KPilot 3.5.6 depends on >=pilot-link-12.0, while pilot-link is buggy for the python and java binding, which is reverse-dependency of JPilot. Anyway, all of them are masked.

Tim told us “Make it work”, let’s roll the sleeves.

In Gentoo’s bugzilla, #89823 is attached an unofficial ebuild for pilot-link, just copy it to pilot-link-0.12.2. Since we don’t really need the python or java binding, just disable the use flag in package.use and leave the patch where is.

Next, build kpilot-3.5.6, it would not compile for GCC 4.1.1. Em, interesting. The bug is straight-forward anyway, the compiler is just picky in doing const cast. With this patch, it works.

UPDATE Just contacted with kpilot developer, pilot-link has changed the interface in the version bump, so kpilot 3.5.6 is supposed to work fine with pilot-link-0.12.[01], this patch is for pilot-link-0.12.2 only.

Let’s put the pieces together:
pilot-link-0.12.2 ebulild, kpilot-3.5.6 ebuild, kpilot-3.5.6-const.patch

Now, KPilot 3.5.6 works fine, but put duplicated appointment when HotSync with Treo 650. That is quite annoying, I would like to use Undup as the work around, then dig into the code later.

One is better than two

Palm February 9th, 2007

After 2 weeks eBay hunting, I won a unlocked Treo 650 GSM smartphone eventually, I no longer need to carry my shabby Tungsten T and Nokia phone, one is better than two.

This gadget is powered by PalmOS v5.4, Treo Software 1.7b, Firmware 1.51. Palm Inc has recently released one ROM update for the unlocked phone, SW 1.20 for bug fix. I decided to keep up with the service pack. The stability of Treo 650 has always been an issue in the sphere.

I chose the ROM from Hi PDA forum, the localization and candies for the newbies have been ripped off to optimize the DB Cache. Download, unpack, copy to the SD Card, hard-reset the device, plugin the card, pray, pray…, after several reboot, the device is updated and the phone is still usable, unlocked.

Treo 650 phone info