[Gllug] OT(ish): Advice

Nix nix at esperi.demon.co.uk
Sun Dec 15 18:17:36 UTC 2002


On Thu, 12 Dec 2002, Jonathan Harker moaned:
> On Thursday 12 Dec 2002 12:05 am, rich at annexia.org wrote:
>> On Wed, Dec 11, 2002 at 08:44:11AM +0000, Nix wrote:
>> > ... and what to do when designing a framework of generic algorithms and
>> > containers.
>> >
>> > Your challenge: rewrite the STL above a language with less expressive
>> > power (other than Ada, where it's been done, mostly). :)
>>
>> ----------------------------------------------------------------------
>> #include <vector>
>> #include <algorithm>
>> main ()
>> {
>>   vector<int> v;
>>   v.push_back (10);
>>   vector<int> w;
>>   copy (v.begin (), v.end (), w);
>> }
> 
> <disclaimer>
> I don't want to start a C++ vs. world+dog jihad. BUT...
> </disclaimer>
> 
> First of all, C++ does not assume int, and hasn't for over a decade. That is a 
> filthy C habit. Use int main()
> 
> Secondly, don't use an algorithm when you can use a container member function 
> instead, it's more efficient in this case to use vector.copy(). cf. 
> "Effective STL", Scott Meyers, Addison-Wesley.

(Actually, I disagree here; unless profiling shows an inefficiency hit,
I prefer to use the algorithm for ease of movement of code between
different containers. Your code should ideally not care what container
it's implemented above, but should just care about iterators --- and
algorithmic complexity of course.)

Thirdly, that program isn't C++. Either add `using namespace std', or
s/vector/std::vector/ throughout.

And RTFM the g++ manual at
<http://gcc.gnu.org/onlinedocs/libstdc++/19_diagnostics/howto.html#3>,
which describes _GLIBCPP_CONCEPT_CHECKS, which significantly improve
diagnostic quality (although perhaps not in that case).

> Complaining about compiler warnings in poorly written C++ programs does not 

... an extremely hard job which should be made substantially easier by Mark
Mitchell's shiny new C++ parser, coming not-terribly-soon in GCC-3.4.

> Anyway, Tet's point was that you can't do anything as powerful, efficient and 
> generally cool as the STL in VB or C# (although with a single inheritance 
> tree the need for templates is lessened).

Inheritance by *actual* interface (as opposed to `by stated interface'
as in Java or via mixins in C++) is something you can't get without
templates; inheritance trees don't change that.

-- 
`One could imagine the Known Net as a conversation between a sentient 
 Ford Motor Company, the New Zealand government, Eric Raymond, and a 
 talking walrus... and probably be not far off the mark.' --- Scott Forbes

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list