l4-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++


From: Bas Wijnen
Subject: Re: C++
Date: Mon, 9 Nov 2009 06:57:06 +0100
User-agent: Mutt/1.5.18 (2008-05-17)

On Fri, Nov 06, 2009 at 10:36:10PM +0100, address@hidden wrote:
> > > Organising things isn't hiding complexity. It's managing complexity
> > > at best.
> > 
> > Whatever you like to call it.  But with this definition, I don't think
> > I actually want to hide any complexity.  In the end, I want to be in
> > control of everything, so it must still be there in a form that I can
> > change.
> 
> My point is that IMHO C++ only limits flexibility,

C++ doesn't limit anything (compared to C).  You can just write the same
code you wrote in C and it will work.

> without really taking anything off my mind.

That's a valid point, but IME it isn't true.  At least it does take
things off _my_ mind. ;-)  But it doesn't limit me at all: I _can_ still
change (and see) all the details of what's happening when I'm
interested.

> Higher-level languages are ones that free me from managing certain
> things by hand.

Yes, but they often also make it impossible to do them by hand as well.
That's what I like about C++: most of the time it is possible to use
laguage (or standard library) features, but it is also possible to tweak
or rewrite them in a way that better fits the problem at hand.

> > > However, I'm not talking about runtime features, nor explicit code
> > > generation. I'm talking about the type inference implemented by some
> > > languages (mostly functional ones), allowing the compiler to
> > > automatically instance any function for the types it is actually
> > > used on,
> > 
> > But that's exactly what templates do (and what C is incapable of).
> 
> Eh? While I'm not really familiar with templates (they didn't exist when
> I was learning C++), my understanding is that I have to write a template
> instead of a "normal" function, and then explicitely ask for specific
> functions being instanciated from this template for the types I need.
> This explicit template plus instanciation feels like a kludge to me,
> when it is perfectly possible (in other languages) for the compiler to
> figure out the necessary types on its own.

I'll give a short description of how templates work, see for yourself if
this is what you thought. :-)

For example, when I define a template function like this:

template <typename T> T &minimum (T a, T b)
{
  return a < b ? a : b;
}

I can simply call the function as minimum (3, 6);, and it will work with
any type that has operator< defined.  Only if the compiler cannot know
what type you want, do you need to specify it.  For example, if I want
to define a std::vector (dynamically allocated array), I cannot say

std::vector a;

Because the compiler doesn't know what to instantiate.  So I need to say

std::vector <unsigned> a;

to create a vector of unsigneds.  The only way to not name the type
here is if every type is derived from the same base class, and all
variables are pointers.  Python and javascript both work like that.  It
means that you don't need to specify the variable type, you can just
pass a "variable", and figure out what type it was later.  I don't like
this feature, because it turns compile-time errors into runtime errors.
I do see the benefits as well, but they don't outweigh the costs to me.

Thanks,
Bas

Attachment: signature.asc
Description: Digital signature


reply via email to

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