Archived

This topic is now archived and is closed to further replies.

What's wrong with C++ templates?

This topic is 5307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

From kuro5hin.org, What''s wrong with C++ templates.
quote:
...just because templates are the best C++ has to offer doesn''t mean they''re good. It turns out that all the problems templates solve were already solved better, before templates were ever introduced, by other languages. In fact, the kludgey way templates work precludes C++ programmers from a lot of the good stuff that nicer solutions have.

Share this post


Link to post
Share on other sites
quote:
Original post by Estor
Please, do not start next topic like this.

Like what?
quote:

Better use search button, and keyword is "template"

And what use will that be for sharing an interesting article with the rest of the community?

Share this post


Link to post
Share on other sites
I don''t see anything on the search results that looks like SabreMan''s (very interesting) link...

Anyway, didn''t we know all along that Lisp is far superior?

Share this post


Link to post
Share on other sites
Is this going to be a discussion of why you should use a different
language if you want to do the kind of things c++ templates are
trying to do?

Or is it going to be a discussion of how templates are designed in
c++ and could have been designed better, querying the reasons why
the standards committee agreed to specify them as they are?

Or is it going to be a discussion of how generics, such as are in
Python or Lisp, are simply too far removed from a static,
non-introspective language like c++ to be feasible.

Or is it going to be a discussion of how to best use templates in
C++ and the situations where it would be advisible to switch to
another language? After all they're far from useless.

Or is it going to be a slanging match?

Or something else?

It's up to you!

[edited by - petewood on June 3, 2003 8:57:16 AM]

Share this post


Link to post
Share on other sites
I don''t consider that article a "what''s wrong with templates" article. It''s more of a "history of generic and compile-time programming" article.

Either way, if you want to complain about C++ templates, then I guess you can go write your games in Lisp...

Share this post


Link to post
Share on other sites
quote:
Original post by fizban75
Either way, if you want to complain about C++ templates, then I guess you can go write your games in Lisp...


How boring this place would be with no room for discussion!

Share this post


Link to post
Share on other sites
quote:
Original post by fizban75
I don''t consider that article a "what''s wrong with templates" article. It''s more of a "history of generic and compile-time programming" article.

Do you understand the statement `templates are really just a C++ version of Lisp macros geared towards generating type declarations rather than extending C++''s syntax''?
quote:

Either way, if you want to complain about C++ templates, then I guess you can go write your games in Lisp...

You could do that. Or you could just stick your head in the sand and never try to find better ways of doing things.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
Do you understand the statement `templates are really just a C++ version of Lisp macros geared towards generating type declarations rather than extending C++''s syntax''?


Like I said, this doesn''t say what''s wrong with C++ templates or that they don''t do what they were meant to do. It just says that they are similar to something that has been done in another language. I didn''t seen anything in that article that shows that C++ templates are "wrong" but just that they do things in a different way than what''s been done in other languages. Every language is different, each with it''s own costs and benefits and you should just choose the best tool for your job.

quote:
You could do that. Or you could just stick your head in the sand and never try to find better ways of doing things.


Wow, what an enlightened discussion we''re having. Such an adversarial tone, tsk, tsk. How old are you? Ten?

Share this post


Link to post
Share on other sites
quote:
Original post by fizban75
Like I said, this doesn''t say what''s wrong with C++ templates or that they don''t do what they were meant to do.

The article describes some things which people commonly do with C++ templates, and goes on to explain how various other systems cope better with the demands that are often put on C++ templates.
quote:

It just says that they are similar to something that has been done in another language.

It doesn''t *just* say that. The author makes it fairly clear that he considers them inferior. Have you used any of the alternative systems he mentions?
quote:

I didn''t seen anything in that article that shows that C++ templates are "wrong" but just that they do things in a different way than what''s been done in other languages.

The implication is supposed to be that they are `wrong'' because they solve a problem for which solutions were already available in a manner inferior to those solutions.
quote:

Wow, what an enlightened discussion we''re having. Such an adversarial tone, tsk, tsk.

It was a suitable response to your suggestion `if you want to complain about C++ templates, then I guess you can go write your games in Lisp...''.
quote:

How old are you? Ten?

What would it matter if I am? Are you interested in discussing the contents of the article or not?

Share this post


Link to post
Share on other sites
Yes, I''d love to discuss the content of the article, but since I have my head in the sand, that may be difficult.

So, the article says C++ templates are inferior to Lisp. Fine. So what should we do? I see two options.

1) Make C++ have better generic programming support. (Any ideas? Because of the strong typing in C++, it doesn''t seem to me that there''s a way other than templates without removing the type constraints.)
or
2) Write our programs in Lisp. Which is what I said originally and caused you to tell me to act like an ostrich.

Share this post


Link to post
Share on other sites
Well the obviously interesting option to explore is 1., since with 2. all discussion basically ends there! (or rather, moves on to the drawbacks of using Lisp for game programming)

Is there another compiler model for C++ that would allow it to retain it''s trademark typing, while relaxing some of the code emission constraints? Comeau''s troubles trying to implement the template "export" keyword seems to suggest it''s not possible.

Then, is there a way to somewhat relax the typing of C++ without making a half-assed Lisp out of it?

Share this post


Link to post
Share on other sites
Some people are trying to find the smaller/cleaner/better language hidden in C++ and trying to clean up the syntax and generally improve it.

fizban, you can't fix problems until you recognize them and face them.

I hope some of the people involved in Artois decide to join this discussion either here or on kuro5hin.

Perl 6 post on a similar topic (from the kuro5hin messageboard).

[edited by - flangazor on June 3, 2003 11:31:53 AM]

Share this post


Link to post
Share on other sites
Here''s the thing. I hear people saying that something is wrong with C++ templates. I however, don''t see that, nor do I see how that article can say that because it''s comparing two highly different things: A strongly-typed language (C++) vs dynamically-typed languages (ML and Lisp).

So, to say that C++ templates are wrong is to say that strong-typing is wrong.

In order to "fix" C++ templates, you have to remove the strong-typing. I don''t want to do that, because that''s something I like about C++. If I want complete dynamic typing, I''ll write in Python, Perl, Lisp and whole suite of languages that do things that way.

C++ was not meant to be dynamically typed the way those other languages are, nor should it, IMO. If you want dynamic typing, you should choose those other languages. In fact, you can write games in Python and get the dynamic typing. I, however, will continue to write in C++, because I personally prefer that language.

So again, I''ll say, if you don''t like the way C++ does generic programming, you are free to choose another language that does it differently. Remember, it''s different, not right or wrong.

Share this post


Link to post
Share on other sites
quote:
I hear people saying that something is wrong with C++ templates. I however, don''t see that, nor do I see how that article can say that because it''s comparing two highly different things: A strongly-typed language (C++) vs dynamically-typed languages (ML and Lisp).

Lisp is strongly typed. Judging from your post, you may be confusing strong typing with static typing. Static typing and dynamic typing are opposites. Strong typing and weak typing are opposites.

quote:
1) Make C++ have better generic programming support. (Any ideas? Because of the strong typing in C++, it doesn''t seem to me that there''s a way other than templates without removing the type constraints.)

The conclusion of the article says:
quote:
For one thing, templates hurt the C++ compiler''s ability to generate efficient code. It might surprise you to hear that, considering that C++ is "efficient" while functional languages like ML and Scheme are "inefficient," but it''s true. C++ templates hide your intentions from the compiler: did you mean type abstraction? Did you mean a syntax extension? Did you mean constant-folding or compile-time code generation? The compiler doesn''t know, so it has to just blindly apply the copy-and-paste strategy and then see what happens. In ML or Scheme, though, aside from the individual benefits described above, the simple fact that you''re telling the compiler what you want to achieve lets it optimize for you much more effectively.


Share this post


Link to post
Share on other sites
quote:
Original post by fizban75
Here''s the thing. I hear people saying that something is wrong with C++ templates. I however, don''t see that, nor do I see how that article can say that because it''s comparing two highly different things: A strongly-typed language (C++) vs dynamically-typed languages (ML and Lisp).

ML is statically-typed.
quote:

So, to say that C++ templates are wrong is to say that strong-typing is wrong.

Please try and be consistent in your terminology. Strong (or strict) typing is not the same thing as static typing. Keeping with strict typing, it might be a legitimate question to ask whether it is `wrong'', particularly in the context of metaprogramming. It might also be legitimate to say something like a hybrid approach to metaprogramming is a good idea, where perhaps a Lisp system is used as a kind of preprocessor for generating generic C++ code. Or maybe use Lisp for the bulk of development, whilst interfacing to C++ components via FFI where desirable performance cannot be gained with Lisp. Your attitude precludes such lines of discussion.
quote:

So again, I''ll say, if you don''t like the way C++ does generic programming, you are free to choose another language that does it differently. Remember, it''s different, not right or wrong.

I already write much of my s/w using dynamically-typed languages. That doesn''t mean I''m uninterested in discussing how C++ templates might be improved, or alternative approaches to generative s/w development.

Share this post


Link to post
Share on other sites
Sorry for the terminology mixups.

I would agree that metaprogramming with templates is a "kludge" and I wouldn''t be adverse to see it replaced with something like you suggest. However, I don''t think that this wrongness is something that can be attributed to templates themselves, but more to those that use them for this purpose, since that wasn''t their original intention.

BTW, my "attitude" was a result of you telling me to stick my head in the sand, which I found inappropriate.

Share this post


Link to post
Share on other sites
I love C++, and I love templates. I have no problems or arguments with them (except for the lack of the export keyword).

Of course, I tend to not do much meta-programming as is, so the vast majority of my template use is for convenience and to avoid some code re-use.

[edited by - daerid on June 3, 2003 12:33:21 PM]

Share this post


Link to post
Share on other sites
quote:
For one thing, templates hurt the C++ compiler''s ability to generate efficient code. It might surprise you to hear that, considering that C++ is "efficient" while functional languages like ML and Scheme are "inefficient," but it''s true. C++ templates hide your intentions from the compiler: did you mean type abstraction? Did you mean a syntax extension? Did you mean constant-folding or compile-time code generation? The compiler doesn''t know, so it has to just blindly apply the copy-and-paste strategy and then see what happens. In ML or Scheme, though, aside from the individual benefits described above, the simple fact that you''re telling the compiler what you want to achieve lets it optimize for you much more effectively.


This statement (from the article) is incorrect. Copy-paste isn''t the last step of template code generation. The compiler still has the opportunity to optimize said code as it sees fit. Template expression objects tend to produce code as optimal as you''re going to get with a particular set of operations.

Articles like this one tend to not present underlying assumptions upfront. In this case, the assumption is "Execution speed is less important than code expressivity/maintenance/beauty." While the upward trend of CPU speeds and memory size tends toward this assumption, it is not always valid in every domain. I wish these articles would acknowledge that when decrying certain features of C++.

MSN

Share this post


Link to post
Share on other sites
quote:
Original post by msn12b
This statement (from the article) is incorrect. Copy-paste isn't the last step of template code generation.

I think what the author is referring to is that for n instantiations of a templated entity T, n similar pieces of code will be put into the final image. This is a fairly well known problem with C++, and leads to solutions such as using void* generic code, with templated type-safe wrappers.
quote:

The compiler still has the opportunity to optimize said code as it sees fit.

It's fairly unlikely that the compiler can generate one piece of code for two template instantiations for types having incompatible layouts.
quote:

Articles like this one tend to not present underlying assumptions upfront. In this case, the assumption is "Execution speed is less important than code expressivity/maintenance/beauty."

That's often a given in many such articles, but the quote you provide suggests an area where C++ might be less efficient than the alternatives. Personally, I feel there's a tendency to assume that C++ is always more efficient than all the alternatives, and nobody seems to question this assumption. Why shouldn't ML or Lisp or Haskell compilers be able to generate more efficient code than a C++ compiler?

[edited by - SabreMan on June 4, 2003 6:27:05 AM]

Share this post


Link to post
Share on other sites
Duplicating the code like that is probably much more efficient in terms of CPU time, as you get the possibility of different specific optimisations being applied to the different types in each case, and the isolation of any type or value-based despatch decisions to the start of the algorithm''s execution. The downside is the increased executable size which -may- cause extra cache misses, etc.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
The downside is the increased executable size which -may- cause extra cache misses, etc.



Code cache misses happen most often in long loops or highly non-predictable conditional (complicated) code. If the bottlenecks of the app have highly cacheable code behavior, executable size will not have an overtly averse effect on the code cache.

MSN

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
It''s fairly unlikely that the compiler can generate one piece of code for two template instantiations for types having incompatible layouts.



This is true, but misleading. Duplicated code, in and of itself, does not hurt performance. Code that causes cache thrashing, e.g., due to excessively long loops or complex branching, will hurt performance.

quote:

Personally, I feel there''s a tendency to assume that C++ is always more efficient than all the alternatives, and nobody seems to question this assumption. Why shouldn''t ML or Lisp or Haskell compilers be able to generate more efficient code than a C++ compiler?



Without heavy preprocessing or JIT compiling, the level of abstraction in the execution model of ML and Lisp will be the source of most performance issues.

MSN

Share this post


Link to post
Share on other sites
quote:
Original post by msn12b
Without heavy preprocessing or JIT compiling

You do know that they are already compiled languages?
quote:

the level of abstraction in the execution model of ML and Lisp will be the source of most performance issues.

That''s just hand-waving. Please be specific about why ML and Lisp should be less efficient. If you can''t think of specific reasons, then simply say that you do not know.

Share this post


Link to post
Share on other sites