Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2001:
[Freeciv-Dev] Re: curiosity
Home

[Freeciv-Dev] Re: curiosity

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Andrew Sutton <ansutton@xxxxxxx>
Cc: Petr Baudis <pasky@xxxxxxxxxxx>, Reinier Post <rp@xxxxxxxxxx>, Freeciv developers <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] Re: curiosity
From: Raimar Falke <hawk@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 2 Dec 2001 18:09:28 +0100
Reply-to: rf13@xxxxxxxxxxxxxxxxxxxxxx

On Sun, Dec 02, 2001 at 11:46:58AM -0500, Andrew Sutton wrote:
> templates can be ugly if they aren't treated carefully. it can make code look 
> worse too. for example the declarations of an iterator for a map:
> 
> std::map< unsigned, FC_Unit * >::iterator i;
> 
> however, this can be significantly simplified using a typedef.
> 
> typedef std::map< unsigned, FC_Unit * > Unit_Map;
> Unit_Map::iterator i;
> 
> it's readable, it looks nice, it works nice.
> 
> as for code bloat with templates, that's true... sort of. it's a tradeoff. a 
> template class adds absolutely NO overhead when it's defined. however, when 
> it's instantiated, it defines and instantiates a new class - thereby adding 
> to the size of the code. however, the alternative would be to define a new 
> class for every template that is instantiated. consider a list class (not 
> STL). there's 2 ways of defining lists.
>       1. templates
>       2. inheritance
> 
> with templates, you just instantiate the class and a new instance of the 
> class is created.
>       list< int > a;
> 
> with inheritance, you define a base list class and derive instances of it.
> 
> class list {};
> class int_list : public list {};
> 
> see? tradeoff. either way, you end up with more code. both solutions provide 
> typesafe containers. however, the inheritance implementation means actually 
> writing those classes and increases the possiblity of errors in the code.

And now compare this to common/speclist and common/genlist. These
provide typesafe access without extra bloat.

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 "Just because you put a flag on the moon doesn't make it yours, it just
  puts a hole in the moon."


[Prev in Thread] Current Thread [Next in Thread]