Prev: C++ analog of setvbuf() ?
Next: Need to analyze C source code and determine loops (program analysis)
From: restor on 8 Dec 2009 07:37 > Subject says it all really. I have been in Javaland for a while where > you do have this capability. I am aware of Meyers Effective STL item 2 > ("beware of the illusion of container-independent code") but I don't > think item 2 applies when all you want to do is iterate over a > container. This is the simple case that people want to do most of the > time (IMHO). Of course if you want to do something else like reverse > iterate etc then with C++ you can. But it does seem mysterious to me > why it is more awkward to do a generic iteration for the most common > case. Hi, it is not clear what you mean by "generic iteration." Suppose all containers did inherit from a common type. Could you give an example how this "generic iteration" would look like? Regards, &rzej -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Joe Gottman on 8 Dec 2009 07:39 Andrew wrote: > Subject says it all really. I have been in Javaland for a while where > you do have this capability. I am aware of Meyers Effective STL item 2 > ("beware of the illusion of container-independent code") but I don't > think item 2 applies when all you want to do is iterate over a > container. This is the simple case that people want to do most of the > time (IMHO). Of course if you want to do something else like reverse > iterate etc then with C++ you can. But it does seem mysterious to me > why it is more awkward to do a generic iteration for the most common > case. > > I realise that in the upcoming standard the use of auto to declare the > iterator will make it slightly easier. C++ distinguishes between run-time polymorphism (from inheritance) and compile-time polymorphism (from templates). The STL was built around compile-time polymorphism for several reasons: 1) It's faster. Small template functions are easily inlined for added speed. 2) It can be used with non-class objects. Under your scheme, all iterators and containers would have to inherit from some abstract Container and iterator classes, but the STL algorithms work fine with built-in arrays and pointers, which don't inherit from anything. Also, vector iterators are often pointers. Joe Gottman -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 8 Dec 2009 07:30 Andrew wrote: > Subject says it all really. I have been in Javaland for a while where > you do have this capability. I am aware of Meyers Effective STL item 2 > ("beware of the illusion of container-independent code") but I don't > think item 2 applies when all you want to do is iterate over a > container. This is the simple case that people want to do most of the > time (IMHO). Of course if you want to do something else like reverse > iterate etc then with C++ you can. But it does seem mysterious to me > why it is more awkward to do a generic iteration for the most common > case. All the standard containers provide begin(), end(), and typenames [const_]iterator (and value_type), and the iterators share the common interface (operator*() and operator++()). With these you can write for (C::iterator i = c.begin(); i != c.end(); ++i) { C::value_type& v = *i; // ... } for every standard container type C and a container object c of type C. Isn't this a "generic iteration"? What makes you think the standard containers don't allow generic iteration? # To be pedantic, what you're using is probably not "STL containers" but "standard containers", even though the latter originated from STL. -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Frank Bergemann on 8 Dec 2009 08:55 Andrew schrieb: > Subject says it all really. I have been in Javaland for a while where > you do have this capability. I never understood the idea to have an inheritance tree, where everything has a common root type of object. A teacher of a C++ course(!) was telling in a break, that he likes the this inheritance approach - he told, that java is more consequent in this. But i was argueing, that i can't follow this approach. Because it's is a matter of selection - based on purpose - which distinctions/extensions you do and on what level in a hierarchy. There is nothing like a common (static) structure, which fits everything. > I am aware of Meyers Effective STL item 2 > ("beware of the illusion of container-independent code") but I don't > think item 2 applies when all you want to do is iterate over a > container. And this is a SINGLE issue, that you address here! BTW: in C++ there is no implementation for iteration in the container itself. There is _support_ for this provided by the container class. But adapters (iterators) are used to facilitate for this. I think it's an example for the FTSE (fundamental theorem of software enginnering) which applies here: another level of indirection. The combination is the solution and it offers more variants. > This is the simple case that people want to do most of the > time (IMHO). Of course if you want to do something else like reverse > iterate etc then with C++ you can. But it does seem mysterious to me > why it is more awkward to do a generic iteration for the most common > case. You might want implement your own iterator for some purpose - to play nicely with an STL container. > I realise that in the upcoming standard the use of auto to declare the > iterator will make it slightly easier. a matter of taste. C++ is a language, where you wanna know, what's going on. Auto IMHO is using the help of the compiler for writing (generic) code easier for dependent types. > > Regards, > > Andrew Marlow > rgds, Frank -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: peter koch larsen on 8 Dec 2009 09:20
On 7 Dec., 20:52, Andrew <marlow.and...(a)googlemail.com> wrote: > Subject says it all really. I have been in Javaland for a while where > you do have this capability. I am aware of Meyers Effective STL item 2 > ("beware of the illusion of container-independent code") but I don't > think item 2 applies when all you want to do is iterate over a > container. This is the simple case that people want to do most of the > time (IMHO). Of course if you want to do something else like reverse > iterate etc then with C++ you can. But it does seem mysterious to me > why it is more awkward to do a generic iteration for the most common > case. > > I realise that in the upcoming standard the use of auto to declare the > iterator will make it slightly easier. So what is it you want to do that you can't do with a simple template- function (that is probably already made for you and can be found in e.g. <algorithm>)? Requiring iterators to have virtual functions would not only increase the memory- and cpu-footprint of your program; it would also prohibit using ordinary pointers as iterators and require the elements themselves to be derived from a common baseclass. If not, how would you dereference an iterator? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |