 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Eugene Gershnik Guest
|
Posted: Sun Oct 05, 2003 12:37 am Post subject: C++ templates vs. .NET generics |
|
|
Take a look at the
http://msdn.microsoft.com/msdnmag/issues/03/10/NET/default.aspx
Is the "run-time type expansion causes less code bloat" argument given there
a bogus one or there is something behind it?
BTW my favorite BS pearl from this article is:
"Of all the fringe benefits of run-time type expansion, my favorite is a
somewhat subtle one. Generic code is limited to operations that are certain
to work for any constructed instantiation of the type. The side effect of
this restriction is that CLR generics are more understandable and usable
than their C++ template counterparts."
Eugene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
WW Guest
|
Posted: Sun Oct 05, 2003 5:50 pm Post subject: Re: C++ templates vs. .NET generics |
|
|
Eugene Gershnik wrote:
| Quote: | Take a look at the
http://msdn.microsoft.com/msdnmag/issues/03/10/NET/default.aspx
Is the "run-time type expansion causes less code bloat" argument
given there a bogus one or there is something behind it?
BTW my favorite BS pearl from this article is:
"Of all the fringe benefits of run-time type expansion, my favorite
is a somewhat subtle one. Generic code is limited to operations that
are certain to work for any constructed instantiation of the type.
The side effect of this restriction is that CLR generics are more
understandable and usable than their C++ template counterparts."
|
IMHO the question is: do you prefer runtime or compile time errors. I
prefer the latter. And IMHO the above question answered the wrong way is a
sort of mistake like the preference of buffer overruns vs. 0.2% slower
programs. I do not think if they do check if the generic instantioation
makes sense compile time - only they generate it runtime. If that unlikely
case would be the truth - then the wuestion is: do you prefer something to
be done once, or million times.
C++ templates and code bloat. C++ compiler vendors (especially Microsoft)
are just getting template *functionality* right. And we all know
optimization can only come after that. In theory C++ compilers/linkers
could do an awful lot of optimization there. For example generating the
very same code for int and long if they are the same, and refer to this with
two names. Split up the templates (compile time) into type independent and
type-dependent parts and only generate one instance of the type independent
parts etc.
I am anyway be very very suspicious getting absolute statements about the
quality of C++ templates from a shop, which did not manage to get them right
for many years, or even attempt to implement them (export aside) for very
recently. My point is: nothing whatsoever proves that those guys bashing
C++ templates in the article know/understood what C++ templates are.
I find it rather tiring that the center of the Solar system as well as the
inventor of the keynote keeps comparing VM based (not far from interpreted)
languages to C++. C++ is the language to write a portable VM in. Java and
C# are languages running on a portable VM.
In dynamic languages (such as Java and C#) it is perfectly possible to write
"self-modifying" code. Just like in CA-Clipper it was possible to create
completely new classes runtime. What we see above is pretty close to this.
"We have postponed all bindings generics and to runtime, so you have a huge
freedom to write your code". But this also means "we have postponed nearly
all error handling to runtime, so basically it is all up to you." Since it
is proven a software cannot be fully tested, this is a recipe for chaos(*).
Also, since the work is postponed to runtime (whether it is needed to be or
not), it has effect on speed. Furtermore some of those cannot be done only
once. If I have a component loaded/released dynamically then I have to
generate the generics each time I re-load it. And even if I keep it in
memory I have to check if I have generated it or not (this might be very
cheap, actually zero overhead above the usual dynamic name binding).
Also the attempt to hide the complexity from the programmer ends up as
nightmare when looking at real life products. CA-Clipper programmers using
LOCATE (sequential search) instead of SEEK (indexed search) because all the
complexity was hidden from them. Next thing was that people started to make
"query optimizing database drivers". Which is not bad, except that users
started to index all fields becase "that will make all my queries fast" and
then it took ages to insert a record...
I am not saying that C++ strikes a good balance on much of the complexity is
shown. But hiding has to have a limit. And automation has to have a limit.
Or a backdoor to good old manual (see professional cameras, see my "proposal
in adding resource handlers to gc").
But making people feel (via marketing and product documentation) that
playing ball with an egg or a softball or a piece of rock is the same is
simply irresponsible. Everything has a price and people who do not know
what they are doing or what things they trigger into action will just be
ignorant of the consequences, therefore unable to reliably create value.
I have one question I always wanted to ask of these guys who sell their
languages by bashing C++. I mean I wanted to ask the question so that they
can answer it for themselves. If C++ is so bad, why does the designers of
every new OO (or multiparadigm) language try so hard to prove that their
language is better? If C++ is so bad, why is it important to prove that
your product is better?
(*) Unless oyu have very experienced and disciplined programmers. But the
whole idea of such languages is that the programmer does not need to be
such, since the language is "safe".
--
WW aka Attila
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Igor Ivanov Guest
|
Posted: Mon Oct 06, 2003 11:36 am Post subject: Re: C++ templates vs. .NET generics |
|
|
"Eugene Gershnik" <gershnik (AT) nospam (DOT) hotmail.com> wrote
| Quote: | Take a look at the
http://msdn.microsoft.com/msdnmag/issues/03/10/NET/default.aspx
Is the "run-time type expansion causes less code bloat" argument given there
a bogus one or there is something behind it?
BTW my favorite BS pearl from this article is:
"Of all the fringe benefits of run-time type expansion, my favorite is a
somewhat subtle one. Generic code is limited to operations that are certain
to work for any constructed instantiation of the type. The side effect of
this restriction is that CLR generics are more understandable and usable
than their C++ template counterparts."
|
Not BS but rather a mild statement of the fact that C++ templates are
used for the purpose for which they are poorly suited.
Igor
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Glen Low Guest
|
Posted: Mon Oct 06, 2003 12:43 pm Post subject: Re: C++ templates vs. .NET generics |
|
|
| Quote: | Is the "run-time type expansion causes less code bloat" argument given there
a bogus one or there is something behind it?
|
Compiler and linkers could do a lot to reduce this. By the ODR,
vector<int> should be the same anywhere in the program, and thus is a
case for the compiler or linker not to repeat the code thereof (not
really a limitation of the language per se).
| Quote: | "Of all the fringe benefits of run-time type expansion, my favorite is a
somewhat subtle one. Generic code is limited to operations that are certain
to work for any constructed instantiation of the type. The side effect of
this restriction is that CLR generics are more understandable and usable
than their C++ template counterparts."
|
They've adopted the same route as GJ of using interface constraints on
the allowable types for their template parameters. This should improve
compile-time checking since the compiler can now verify the
correctness of the template vs. the interface, but ties their generic
implementation back to rigid type hierarchies, which was one of the
freeing things about C++ templates.
Regards,
Glen Low, Pixelglow Software
www.pixelglow.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Tue Oct 07, 2003 1:42 am Post subject: Re: C++ templates vs. .NET generics |
|
|
Eugene Gershnik wrote:
Well, I think it depends on the generic code you write. If your class
depends essentially on generic contraints I doubt that you will be able
to be much better than C++ code: the code differs significantly between
each instantiation. On the other hand, if you have no constraints or
only non-generic ones, the generic code becomes essentially handling of
some pointers with fixed types plus a few casts. Of course, in both
cases C# and C++ can do essentially the same: nothing in the first and
delegate to a common implementation in the second case. It would also
be interesting to see the performance of complex generic algorithms
being compared to C++ implementations: With optimization turned on,
compilation of templates with heavy use of inline functions can take
ages but the resulting could be blindingly fast. With the
parameterization (ie. effectively algorithm configuration) done at
run time this seems to ask for slow performance: either the optimizer
eats the time or the execution of the algorithm. Of course, if the
algorithm is called often after being optimized just once, it could be
similar to C++ templates.
After answering your immediate question, a few thoughts in general:
| Quote: | From a first glance (I have just read the two article and this is all
information I have about C# generics) the constraints approach seems |
to have benefits over the C++ approach. In particular, it effectively
avoids the whole name look-up issue (in particular argument dependent
lookup). On the other hand, I'm not yet sure whether the constraints
approach is itself constraining: in generic code it is important that
eg. return types can be dependant on the function argument types in a
way specific to the arguments. I'm not yet sure that this can be
expressed in generic interfaces.
I'm not sure whether I simply missed it but apparently [partial]
specialization and non-type arguments are not present with generics.
However, both are quite important tools, eg. for recursive templates:
often, there is a generic version for the general case but it builts
upon a special case. For example, an n-dimensional array can be seen
as an array of (n-1)-dimensional arrays - except for 'n == 1' in
which case it is simply an array of objects. But don't get distracted
by this example: there may be solutions to this particular issue but
the problem is much more general.
In general, CLR generics seem to be less powerful than C++ templates.
On the other hand, this may be due to the audience of the articles I
have read: there is no point of confusing people unaware of generic
approaches with the finer but also essential points. I should look
for a more thorough description of CLR generics...
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Graham Batty Guest
|
Posted: Tue Oct 07, 2003 3:27 pm Post subject: Re: C++ templates vs. .NET generics |
|
|
"Eugene Gershnik" <gershnik (AT) nospam (DOT) hotmail.com> wrote
I suspect that this is actually largely a misunderstanding of the nature of
both types of generics. In particular, I think this person expects C#
generics to be entirely based on runtime polymorphism. As I understand it,
from the more technical descriptions presented in the article, this is not
the case. In fact, what C# is doing is something that a lot of people feel
C++ should do as well. That is, move template generation to the link phase
to avoid multiple compilation.
Of course, in .net, the link phase is in fact the JIT compiler phase. So
really, I don't know that there is really all that much more benefit
involved here in terms of code bloat (not where it counts, in terms of cache
hits and misses, anyways). What they've done is make generics an integral
part of the .net bytecode, and as such, the JIT compiler will do the work of
instantiating all uses of the template at run time, but not necessarily
DURING run time.
| Quote: | BTW my favorite BS pearl from this article is:
"Of all the fringe benefits of run-time type expansion, my favorite is a
somewhat subtle one. Generic code is limited to operations that are
certain
to work for any constructed instantiation of the type. The side effect of
this restriction is that CLR generics are more understandable and usable
than their C++ template counterparts."
|
I don't know that this is BS. In fact, if I'm right above, it essentially
means that constraints were pretty much necessary. After all, as it stands,
you get pretty crappy errors on templates in C++ (as the author of this
article is overly fond of pointing out), but if those templates were
generated at link time, you would end up with far far worse errors. Not only
that, but because C# generics are bytecode themselves, they can really be
external linkage, and so you don't even have the advantage of the code,
which is where the errors would show up, being available in the source file
where the instantiation failed.
So constraints are not so much a feature as a patching misfeature here imo.
Not only that, but it severely limits the application of templates in
general. Your template arguments must provide a set interface (in the
COM/Java/C# sense) or be part of a particular class hierarchy. So in
essense, there really is no generic programming here. Truely, I think C#
generics really are only useful for containers, not for any real sense of
generic programming (in the Stepanov & Lee sense). A step forward, and it
will certainly make the language stronger, but from C++'s perspective, I
think it is in fact a step backwards. I hope that if C++ ever has built-in
constraints, they will be stronger than this in general.
Graham.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daveed Vandevoorde Guest
|
Posted: Tue Oct 07, 2003 7:08 pm Post subject: Re: C++ templates vs. .NET generics |
|
|
Dietmar Kuehl <dietmar_kuehl (AT) yahoo (DOT) com> wrote:
[...]
| Quote: | I'm not sure whether I simply missed it but apparently [partial]
specialization and non-type arguments are not present with generics.
[...] |
That is correct. I was quite surprised when I was first shown
the C# generics syntax which reused the angle brackets that
C++ has had so many problems with. However, without the
possibility of explicit specialization, those angle brackets are
much less of a problem.
Like you say, coming from C++ this feels like a serious loss
of functionality, but when I brought it up, one of the C#
generics designers was convinced that explicit specialization
will never be needed (and as a consequence angle brackets
will never cause parsing trouble).
Daveed
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Chris Perkins Guest
|
|
| Back to top |
|
 |
Jan Bares Guest
|
Posted: Wed Oct 08, 2003 6:20 am Post subject: Re: C++ templates vs. .NET generics |
|
|
| Quote: | Of course, in .net, the link phase is in fact the JIT compiler phase. So
really, I don't know that there is really all that much more benefit
involved here in terms of code bloat (not where it counts, in terms of
cache
hits and misses, anyways). What they've done is make generics an integral
part of the .net bytecode, and as such, the JIT compiler will do the work
of
instantiating all uses of the template at run time, but not necessarily
DURING run time.
|
Hi,
if I understand .NET well, the code bloat will be reduced by the fact, that
the instantiation will be shared across all managed modules in the same
address space. At least in Windows any application is set of many modules
(DLL's etc), without instantiating templates at run time you cannot share
them.
Best regards, Jan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Wed Oct 08, 2003 8:53 am Post subject: Re: C++ templates vs. .NET generics |
|
|
Daveed Vandevoorde wrote:
| Quote: | Dietmar Kuehl <dietmar_kuehl (AT) yahoo (DOT) com> wrote:
I'm not sure whether I simply missed it but apparently [partial]
specialization and non-type arguments are not present with generics.
That is correct.
|
I investigated CLR generics more closely, essentially by reading the
paper by the designers and lots of information I found on the internet.
The result is rather disappointing! Things are *much* worse than I had
expected: Essentially, CLR generics are up to avoiding the excessive
use of casts when using containers and that's about it. There is
effectively no support for Generic Programming (which does not
necessarily mean that it is entirely impossible but it would be more
complex than necessary).
Here are the two most important missing features:
- As mentioned, specialization is completely missing - not to mention
partial specialization. This is effectively necessary to cope with
degenerate cases and/or to differentiate between different
capabilities of the parameter types.
- It is hard - if not impossible - to infer associated types, eg. the
return type of some function. However, this seems to be necessary
to spell out the constraints (well, I'm not entirely sure about this
because I haven't found any useful documentation of what is allowed
in the constraints).
STL is out there since eight years, there was loads of discussions on
these issues in the C++ forum, etc. But this experience is plainly
ignored.
| Quote: | I was quite surprised when I was first shown
the C# generics syntax which reused the angle brackets that
C++ has had so many problems with. However, without the
possibility of explicit specialization, those angle brackets are
much less of a problem.
|
Well, the definitely avoid the shift vs. double angle bracket because
there are no non-type template arguments and thus there is no
ambiguity. The issue with angle brackets and and dependent names does
not really surface because it is necessary to provide constraints
which avoid the ambiguity here. I'm not sure I see the particular
problem with explicit specialization. ... but then I'm not sure what
you are refering to exactly: I can picture full specialization or
explicit instantiation but I'm not sure whether either of these terms
is exact.
| Quote: | Like you say, coming from C++ this feels like a serious loss
of functionality, but when I brought it up, one of the C#
generics designers was convinced that explicit specialization
will never be needed (and as a consequence angle brackets
will never cause parsing trouble).
|
Did any of the C# generics designers ever pictured using generics for
more interesting uses than type safe containers? ... and did any of
the C# generics designers ever tried to do any form of Generic
Programming? Unfortunately, it is not really only a problem with C#
but it extends to the whole CLR: types are encoded in MSIL and hence
there is no support for specialization or type inference.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Wed Oct 08, 2003 8:54 am Post subject: Re: C++ templates vs. .NET generics |
|
|
Chris Perkins wrote:
Yes, it seems to give a fairly complete description of CLR generics -
and I was really disappointed. Unfortunately, the article does not really
cover the finer points like what can go into the constraints. But my
expectations are low: they did a good job in preventing use of CLR
generics for Generic Programming.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Boyle Guest
|
Posted: Wed Oct 08, 2003 6:10 pm Post subject: Re: C++ templates vs. .NET generics |
|
|
| Quote: | Yes, it seems to give a fairly complete description of CLR generics -
and I was really disappointed. Unfortunately, the article does not really
cover the finer points like what can go into the constraints. But my
expectations are low: they did a good job in preventing use of CLR
generics for Generic Programming.
|
At the risk of sounding provocative, did anyone expect generics to
support hard-core, Alexandrescu-style template usage? Such support
wouldn't seem consonant with the rest of the design philosophy of the
..NET Framework.
Cheers,
Dave
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Thu Oct 09, 2003 8:48 pm Post subject: Re: C++ templates vs. .NET generics |
|
|
[email]david.boyle (AT) ed (DOT) tadpole.com[/email] (Dave Boyle) wrote:
| Quote: | At the risk of sounding provocative, did anyone expect generics to
support hard-core, Alexandrescu-style template usage?
|
I suppose this is a rhetorical question but it still asks for a reaction:
did anyone *want* generics to support hard-core, Alexandrescu-style
template usage? This stuff is, no doubt, way cool and allows for things
we surely want to take advantage of in C++. C# and the CLR are, however,
a different platform with different techniques and often different goals.
Although I personally enjoy doing cool stuff (well, in some sense
everything I'm doing is "cool" work due to my name the primary
objective is to get the task done. I don't think that advanced template
techniques are necessary for the context of C# and the CLR. However, some
stuff is indeed crucial - and apparently absent.
On the other hand, I have to admit that I'm personally not really that
much interested in creating configurable data structures which seems to
be the primary focus of Alexandrescu and Eisenecker/Czernecki. My focus
is on data structure independent algorithms where "algorithm" refers
effectively to any kind of operation: there is no point in investing in
highly configurable data structures since the crucial knowledge lays in
the algorithms. Data structures are just tossed together... (of course,
I know that there is considerable amount of effort in creating some data
structures but my focus is not at all on this stuff). My personal view
point is that object oriented techniques are well up to data structure
configuration (possibly at the cost of a little bit of performance) but
not at all for algorithms stuff.
| Quote: | Such support wouldn't seem consonant with the rest of the design
philosophy of the .NET Framework.
|
I think that an appropriate set of meta programming facilities could
create a simple to use environment where all the stuff Alexandrescu
address is also possible. How this looks exactly is, however, still
unclear and will probably take still some years of experimentation and
research. After all, meta programming facilities and generic
programming have become popular only relatively recently and are still
immature. Using a pragmatic approach for the .Net framework means to
select a set of easy to use facilities which have a reasonable benefit.
The question is where to draw the line: this is a trade-off between
complexity and benefit. My personal impression is that the current
scope is too narrow to reach beyond the trivial examples and a little
bit more complexity would yield huge benefit. To me it seems that the
team designing C# generics has a strong object oriented background but
no real experience with generic programming: what they are currently
discussion has similar goals and limitations as the first C++ templates
I have seen. This yields some benefit (primarily adding a little bit of
type safety and avoiding some casts) but it is not yet the huge step
forward. However, to make a huge step forward, I think only a tiny
little piece is missing: the capability to infer associated types (what
is clumsily done with nested typedefs in C++). This would not yet yield
the meta programming level we got in C++, it would still be more limited
but it would open the possibility to do quite powerful stuff within the
CLR.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Glen Low Guest
|
Posted: Fri Oct 10, 2003 4:22 pm Post subject: Re: C++ templates vs. .NET generics |
|
|
| Quote: | However, to make a huge step forward, I think only a tiny
little piece is missing: the capability to infer associated types (what
is clumsily done with nested typedefs in C++). This would not yet yield
the meta programming level we got in C++, it would still be more limited
but it would open the possibility to do quite powerful stuff within the
CLR.
|
In practice you would probably use some combination of .NET reflection
and generics to get what you want. It's awkward because it would be
more runtime-based than C++ and thus more prone to errors, but it may
still be serviceable.
For example, in C++:
class outer
{
public:
typedef char inner;
};
outer::inner x;
but in C#:
public class outer
{
public Type inner { get { return typeof (char); } }
};
object x = outer.inner.Create (); // can't remember the exact
method...
With the advent of generics, you probably could hide some of the root
object / casting shenanigans behind a generic interface.
If the metadata support for generics is good (which is what they
claim) and the reflection support for generics is good (my inference),
you can work out something. Full specialization could be achieved
awkwardly by the code checking for the reflected Type and shunting
calls to different implementation object (a la handle/body idiom or
one of the GoF behavior patterns).
I wonder how .NET generics would handle the curiously recursive
inheritance idiom so beloved of their ATL:
public interface X <T> { ... }
public class Y: X <Y> { ... } // no such thing as a fwd declaration in
C#...
Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrei Alexandrescu Guest
|
Posted: Wed Oct 15, 2003 12:43 pm Post subject: Re: C++ templates vs. .NET generics |
|
|
"Jan Bares" <jan.bares (AT) antek (DOT) cz.no.spam> wrote
| Quote: | if I understand .NET well, the code bloat will be reduced by the fact,
that
the instantiation will be shared across all managed modules in the same
address space. At least in Windows any application is set of many modules
(DLL's etc), without instantiating templates at run time you cannot share
them.
|
That is correct. But it's all a tradeoff. Separate instantiation for each
type does produce code bloat, but it's often more efficient.
Actually an impressive system is Pizza
([url]http://pizzacompiler.sourceforge.net/)[/url], which, through a compiler switch,
would select (over the same syntax!) between what they call "homogeneous"
and "heterogeneous" translations. It's sad Pizza didn't make it in Java and
GJ did.
Andrei
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|