Niall's virtual diary archives - May 2003

by . Last updated . This page has been accessed 5,052 times since the 25th October 2003.

 

View this page in: flag English Any language:
flag Chinese flag French flag German flag Japanese flag Portuguese flag Russian flag Spanish

Translation to non-English languages provided by Google Language

You are connecting to the IPv4 version of this website from the IP address 54.162.132.79. You can try the IPv6-only version if you want.

 

 

    You can quickly jump into the index using this quick navigation bar:

Back to archive index

Summary

14th May 2003

Failed the last job interview

Wednesday 14th May 2003: 8.31pm. Got back from the Trolltech interview yesterday - and since they rang up on the Monday night and said not to bother coming in on Tuesday, it was a fairly spectacular failure

The main reason for my rejection was one of the technical tests I had to do for the Oslo interview was to design a C++ list class which worked equally with by-reference members as with by-value members (by-reference means the list is of pointers to the list's contents - by-value means the list actually contains each item in the list)

Obviously, it comes down to somehow distinguishing between value and reference members and treating the value members by copying them into the list - whereas with reference you simply store the pointer to it. I first suggested RTTI (Run Time Type Information) which lets you examine some pointer or value. I was told that was "cheating" and that there was a way of having one list class handle both types automatically.

Traditionally lists in C++ are of the form:

template class List ...

And so to use, you do:

List a; a.append(5);

However, every list implementation has one for values and one for references. In Qt, QValueList and QPtrList. The STL has similar.

I suggested some sort of smart pointer class that could know when it was dealing with pointers or values. Again though, how to distinguish? I suggested it would have something to do with templates, but I couldn't elaborate.

As it turned out, a simple template overload was the solution:

#include <stdio.h>

template<typename T> void getval(T &a)
{
  printf("Ref called\n");
}

template<typename T> void getval(T *&a)
{
  printf("Ptr called\n");
}

template<typename T> class List
{
  public:
  List() {}
  void add(T &v)
  {
    getval(v);
  }
};

int main(void)
{
  List<int> a;
  List<int *> b;
  int c=6;
  a.add(c);
  int *d=&c;                        
  b.add(d);
  return 0;
}

Basically you encapsulate the type specific code into overloads based on the type, and let the compiler assemble the correct implementation for you. Because I didn't determine this at the time, or perhaps how I handled not knowing the answer, I lost the job.

As it happens, they at the interview called this technique "partial template specialisation" which after today's research I now know to be the incorrect term - that's related, but quite different. I remember when I was learning C++ that the going advice at the time was to avoid any trickery with templates at all because they weren't portable - however the above technique is quite simple, it's merely overloading combined with simple template use and nothing more. I can only attribute my lack of thinking of it at the time to being very tired - I even uttered in the interview that their solution worked on the compiler distinguishing between types and pointers to types when this is plainly incorrect - pointers to types are just as much types in themselves and overloading makes no difference between pointers or anything else. I can see now why they said no.

Still, this leaves me bitterly disappointed - as I've said many times, experience has little correlation to ability and the failure to answer the above was more of one with lack of experience with template programming in C++ than lack of ability to comprehend and implement.

One good thing to emerge from this experience is that it seems that the times have moved and now that template support is becoming much more standard, full use of templates will emerge in newly written C++. Full use means more or less you can get the compiler to write much of the code for you in response to a specification - and to I as a lover of functional languages this greatly appeals. It would appear that a book entitled Modern C++ Design by Andrei Alexandrescu is the one to have for this - and at thirty-two quid, it's within my price reach. Here's a link to a review of it.

Well, chances are that by next week, I'll be moving permanently back to Ireland. I can at least say I put everything in I could to find work just as I did to find funding for Tornado. Both have failed, and the end of my career in IT is looming - within a year, I may have become a plumber and perhaps kissed IT goodbye forever. We'll see. Be happy!

Go to next month Go back to the archive index Go back to the latest entries

Contact the webmaster: Niall Douglas @ webmaster2<at symbol>nedprod.com (Last updated: 08 July 2012 20:15:39 +0100)