[Freeciv-Dev] Re: curiosity
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
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."
[Freeciv-Dev] Re: curiosity, Raimar Falke, 2001/12/01
- [Freeciv-Dev] Re: curiosity, Andrew Sutton, 2001/12/01
- [Freeciv-Dev] Re: curiosity, Petr Baudis, 2001/12/02
- [Freeciv-Dev] Re: curiosity, Raimar Falke, 2001/12/02
- [Freeciv-Dev] Re: curiosity, Andrew Sutton, 2001/12/02
- [Freeciv-Dev] Re: curiosity,
Raimar Falke <=
- [Freeciv-Dev] Re: curiosity, Andrew Sutton, 2001/12/02
- [Freeciv-Dev] Re: curiosity, Justin Moore, 2001/12/02
- [Freeciv-Dev] Re: curiosity, Andrew Sutton, 2001/12/02
[Freeciv-Dev] Re: curiosity, Reinier Post, 2001/12/02
[Freeciv-Dev] Re: curiosity, Andrew Sutton, 2001/12/02
[Freeciv-Dev] Re: curiosity, Reinier Post, 2001/12/02
[Freeciv-Dev] Re: curiosity, Raimar Falke, 2001/12/01
|
|