Are C++ templates a precompiler thing?

J

JohnQ

Gavin Deane said:
JohnQ: Can templates be implemented the same way as macros - straight
text substitution?

Yes, unless you want the C++ perversions of the concept. If so, then no.
Smells like C++ lock-in to me.
Others: No - here are some examples of things you can do with
templates that you can't do with macros.

JohnQ: Oh no - I don't mean things that complex or specialised. What I
meant to ask was, "Can a limited subset of template capabilities be
implemented as straight text substitution? The subset is limited to
only those things that can be implemented as straight text
substitution."

That's my definition of templates: a typing saver, patterned
text-replacement. Anything else is a C++-ism (extension of the original
concept). You're assuming the C++ definition is the gold cow standard of all
template definition. That's in-the-box-thinking (paradigmic).
To which the answer is obviously "yes", by definition. But it's a
somewhat pointless question, is it not?

John
 
J

JohnQ

Can they be implemented that way though (early work did exactly that).

"Then they're not templates."

Not "C++ templates" you mean.

"Basically, templates were added to
the language in CFront 3.0 because macros (<generic.h>) were
found to be insufficient."

YMMV.
What's the diff?

"They do two, fundamentally unrelated things. Templates are
integrated into the language, and operate on language level
entities, such as types and constant expressions. Macros
operate at an earlier level, on text."

I prefer the latter.
I disagree. I'm leaning toward "they should be the same".
Beyond that (macros), templates become unwieldly.

"That solution was tried. And failed. Have you ever actually
used <generic.h>?"

I've built on top of that, but I've harvested the ideas there, yes. Putting
the implementation in the preprocessor is better, but I haven't tried that
yet. I agree using macroized lines to define template isn't ideal, but it's
really not that bad either unless you overuse templates. A few hacks to a
preprocessor and it improves. So I'm not that far away from having "99%" of
the functionality that I require for a fraction of the complexity, which is
nice.

John
 
J

JohnQ

"Gianni Mariani" <[email protected]> wrote in message

[...]
HP hacked a preprocessor (PCC) to provide probably the earliest template
implementation for C++.

"HP never hacked the C++ preprocessor to provide templates. Get
your facts straight."

Texas Instruments. Decus C preprocessor. 1990. My bad. The fact that someone
did it though is the important thought.
If I just need to create containers and that is my only
interest in using templates, the specialized stuff you're
doing is, well specialized!

"I've actually used <generic.h> in production code. For
containers and smart pointers. It doesn't work. That's why
templates were invented, because a pre-processor based solution
doesn't work."

Once you get out of the box of thinking that C++ templates are the only
kind, things get easier. YMMV.

John
 
J

JohnQ

"Erik Wikström" <[email protected]> wrote in message
Realize too that if you introduce C++-specific [...]
ones.
"Just a though but isn't the preprocessor quite unportable too?"
(Oh, another one with special characters in their signature line huh).

"And who posts through Google, and who has a header line:
Content-Transfer-Encoding: quoted-printable
From other posts of mine (where I've suppressed the accented
characters), I think that this line is what is causing you
problems. I don't know where it is inserted; Google doesn't
seem to insert it when I post from home. But technically, it is
legal, even if there is no real reason for it to be there, and
it shouldn't cause any problems for your newsreader.

I'm going to try to find out where it is inserted in my case,
and get rid of it if I can. But you really should get your
newsreader fixed. As far as I can tell, you're the only one who
has this problem."

Certainly others are using OE. What's up with mine, if so then, I have no
idea. I do think news should be 7-bit ASCII though when the posts are in
English.

John
 
D

Dizzy

JohnQ said:
That's my definition of templates: a typing saver, patterned
text-replacement. Anything else is a C++-ism (extension of the original
concept). You're assuming the C++ definition is the gold cow standard of
all template definition. That's in-the-box-thinking (paradigmic).

Then you are offtopic to the subject of this discussion. You throw a
question like "Are _C++ templates_ a precompiler thing" then you transform
it to "my definition of templates a typing saver... anything else is a
C++-ism". You see that there is no logic there don't you?

"C++ templates" are "C++ templates" and not your definition of them (and
they are C++-isms my definition).
 
J

JohnQ

Dizzy said:
That's irrelevant to the original question.


If by "templates" you mean something else that ISO C++ template keyword
and
it's related functionality then probably yes, however that's irrelevant to
the original question.


Nobody forces you to use them. Having them is an added bonus. You can just
use the preprocessor you are so found of.


Again not relevant to the original question. The original question was,
quote: "Are C++ templates a precompiler thing?"

This was evolved into "can they be made" a precompiler thing and such. For
the first question you have an easy answer ("no because they are done by
the compiler right now"). For the second there seems to be impossible to
make it in the preprocessor without the compiler knowledge because of the
SFINAE and other template semantics. Of course you may not need those and
of course if your definition of templates resumes to just being some kind
of macros then you could do them with preprocessor macros. However
these "limited templates" are not the "C++ templates" from the original
question, which are much more rich in functionality.

You did say "C++ templates" which means the templates functionality of the
C++ language as a whole.

That's why it's threaded discussion: discussions evolve to give the
information, surrounding and in more detail than was originally asked for.
As the original question was mine, and I know what information I seek, the
drift of the thread away from a curt answer is indeed relevant.

John
 
J

JohnQ

Dizzy said:
Then you are offtopic to the subject of this discussion. You throw a
question like "Are _C++ templates_ a precompiler thing" then you transform
it to "my definition of templates a typing saver... anything else is a
C++-ism". You see that there is no logic there don't you?

"C++ templates" are "C++ templates" and not your definition of them (and
they are C++-isms my definition).

I know what information I am seeking. And that is the plausibility of
implementing templates (not the C++ kind) via modification of the
preprocessor (as a modest improvement over the "macro-ised" implementation
technique). If the answer to the original question was "yes", that would
have made things appear more plausible. Since I'm not enamoured with the
feature set of C++ templates that require templates to be integrated into
the compiler, even though the answer was "no" to the original question, the
plausibility of doing templates (not the C++ kind) with a modified
precompiler remains.

John
 
O

Old Wolf

Are you saying that the phases only are specified and that they are not
associated with things like "preprocessor" and "compiler".
Yes

(I can then make a conforming implementation using preprocessor-based templates?).

Well I'm not really sure what you mean by 'preprocessor-based
templates'.
 
J

JohnQ

Old Wolf said:
Cool.


Well I'm not really sure what you mean by 'preprocessor-based
templates'.

Well I think others have answered that part of it and the answer is no, as
far as I gather. I'm not interested in producing a conforming implementation
anyway. I was just curious. By "preprocessor-based templates", in this
post's context, I mean "implementing templates (not the conforming kind) via
modification of a preprocessor. I also use "preprocessor-based templates" in
referring to the macroized templates implementation using an unmodified
preprocessor and just the text-substitution capability.

John
 
J

James Kanze

I only speak English and recognize the English alphabet, sowwwy.

You mean like in words like "naïve"?
I suppose you want newgroups to be Unicode?

Logically (or perhaps only hopefully), all transmission will be
in UTF-8 one of these days. There *are* places in the world
which don't speak English.
 
J

James Kanze

"James Kanze" <[email protected]> wrote in message
Certainly others are using OE. What's up with mine, if so
then, I have no idea.

You don't know how to configure it? Microsoft is generally
pretty good for internationalization issues.
I do think news should be 7-bit ASCII though when the posts
are in English.

What you think and what things actually are are two different
things. The general tendancy is for everything on the Internet
to be in UTF-8 (although we still have a long way to go). Are
you suggesting that people born in other countries should be
required to change their name in order to participate here? Or
their address?

And the problem doesn't seem to be the characters themselves,
because I modified my .sig in one posting, to remove them. The
problem is, I think, one of the headers, probably inserted by
Google.
 
O

Old Wolf

By "preprocessor-based templates", in this post's context, I
mean "implementing templates (not the conforming kind) via
modification of a preprocessor.

Well, if they're not conforming, then it's a moot
point as to what the standard says about it :)
 
J

James Kanze

I know what information I am seeking. And that is the plausibility of
implementing templates (not the C++ kind) via modification of the
preprocessor (as a modest improvement over the "macro-ised" implementation
technique).

If you don't mean C++ templates, when talking about C++, you
really have to find a different word. And describe exactly what
you mean. All I can say is that in the past, there was an
effort to do something similar, using <generic.h>; anyone who
has actually used it understands why C++ decided to add
templates. The concept really only works well if it is
integrated into the language.
 
J

James Kanze

"James Kanze" <[email protected]> wrote in message
On Jun 12, 2:06 am, "JohnQ" <[email protected]>
wrote:
[...]
If you refer to cfront then that's not true. The output from cfront
just
used C as the "assembler".
HP hacked a preprocessor (PCC) to provide probably the earliest template
implementation for C++.
"HP never hacked the C++ preprocessor to provide templates. Get
your facts straight."
Texas Instruments. Decus C preprocessor. 1990. My bad. The
fact that someone did it though is the important thought.

Did what? <generic.h> predates that by a lot. It proved to be
unusable. Real templates (albeit very buggy) first appeared in
CFront 3.0 (from AT&T), in 1989, I think.
"I've actually used <generic.h> in production code. For
containers and smart pointers. It doesn't work. That's why
templates were invented, because a pre-processor based solution
doesn't work."
Once you get out of the box of thinking that C++ templates are the only
kind, things get easier.

I'm aware of templates in Java (although I've not used them
yet), and in Ada (where they're called generics). There are
lots of alternatives to C++ templates, and there are things in
C++ templates that I'm not too happy with. But they do beat any
macro based solution, by far. I've actually used both, so I'm
in a real position to know.
 
J

James Kanze

"James Kanze" <[email protected]> wrote in message
On Jun 11, 11:11 am, "JohnQ" <[email protected]>
wrote:
"Then they're not templates."
Not "C++ templates" you mean.

Not templates. C++ didn't invent templates; they were present
in languages long before C++ added them. In fact, C++ added
them mainly because the earlier macro-based solution didn't
work.
"Basically, templates were added to
the language in CFront 3.0 because macros (<generic.h>) were
found to be insufficient."

Whatever. The fact remains that a macro based solution was
tried, over a long period of time, by many people of many
different persuations, and was found not to be workable. So the
authors of C++ looked around, and saw that the template based
solution in Ada and Eiffel did work, and "invented" something
similar. Not identical, because of course Ada and Eiffel are
different languages, with a different philosophy, and their
version of templates wouldn't fit in nicely with that of C++.
But the fact remains that templates were added to C++ because
they worked well in other languages, and the pre-processor based
solution which was present before didn't work.
 
D

Dizzy

JohnQ said:
I know what information I am seeking. And that is the plausibility of
implementing templates (not the C++ kind) via modification of the
preprocessor (as a modest improvement over the "macro-ised" implementation
technique). If the answer to the original question was "yes", that would
have made things appear more plausible. Since I'm not enamoured with the
feature set of C++ templates that require templates to be integrated into
the compiler, even though the answer was "no" to the original question,
the plausibility of doing templates (not the C++ kind) with a modified
precompiler remains.

Certainly you can implement a very limited template functionality using only
the preprocessor (as you could probably translate a limited feature C++ to
C using only the preprocessor or few extensions to it).
 
J

JohnQ

"James Kanze" <[email protected]> wrote in message
Certainly others are using OE. What's up with mine, if so
then, I have no idea.

"You don't know how to configure it? Microsoft is generally
pretty good for internationalization issues."

I've never had to venture past the defaults until now perhaps.
I do think news should be 7-bit ASCII though when the posts
are in English.

"What you think and what things actually are are two different
things. The general tendancy is for everything on the Internet
to be in UTF-8 (although we still have a long way to go). Are
you suggesting that people born in other countries should be
required to change their name in order to participate here? Or
their address?"

When in Rome... And when I post in the Chinese groups, I promise to use only
Chinese.

"And the problem doesn't seem to be the characters themselves,
because I modified my .sig in one posting, to remove them. The
problem is, I think, one of the headers, probably inserted by
Google."

What if the header is being inserted because something notices non-7-bit
ASCII characters?

John
 
J

JohnQ

"James Kanze" <[email protected]> wrote in message
On Jun 12, 2:06 am, "JohnQ" <[email protected]>
wrote:
[...]
If you refer to cfront then that's not true. The output from cfront
just
used C as the "assembler".
HP hacked a preprocessor (PCC) to provide probably the earliest template
implementation for C++.
"HP never hacked the C++ preprocessor to provide templates. Get
your facts straight."
Texas Instruments. Decus C preprocessor. 1990. My bad. The
fact that someone did it though is the important thought.

"Did what?"

Hacked an existing preprocessor to implement templates (and exceptions also,
BTW).

" <generic.h> predates that by a lot. It proved to be
unusable. Real templates (albeit very buggy) first appeared in
CFront 3.0 (from AT&T), in 1989, I think."

Unusable to do all the advanced things that "the language within a language"
C++ templates do, yes. And again, it's just some constructs in there that
can be useful. It's not an end.
"I've actually used <generic.h> in production code. For
containers and smart pointers. It doesn't work. That's why
templates were invented, because a pre-processor based solution
doesn't work."
Once you get out of the box of thinking that C++ templates are the only
kind, things get easier.

"I'm aware of templates in Java (although I've not used them
yet), and in Ada (where they're called generics). There are
lots of alternatives to C++ templates, and there are things in
C++ templates that I'm not too happy with. But they do beat any
macro based solution, by far. I've actually used both, so I'm
in a real position to know."

I've used both and now have much better code since abandoning C++ templates
(and yeah, I could refactor the current code back into C++ template syntax
and some of y'all would say it is arguably better, but actually it would be
pretty much the same, only the syntax would be a little different). I tend
to be complexity-averse. YMMV.

John
 
J

JohnQ

I know what information I am seeking. And that is the plausibility of
implementing templates (not the C++ kind) via modification of the
preprocessor (as a modest improvement over the "macro-ised" implementation
technique).

"If you don't mean C++ templates, when talking about C++, you
really have to find a different word."

Well C++ didn't invent the word 'template'.

" And describe exactly what
you mean. All I can say is that in the past, there was an
effort to do something similar, using <generic.h>; anyone who
has actually used it understands why C++ decided to add
templates."

The generic.h thing is just a starting point, not an end all.

"The concept really only works well if it is integrated into the language."

Only if by "concept" you mean "all the bells and whistles of C++ templates".

John
 
J

JohnQ

"James Kanze" <[email protected]> wrote in message
On Jun 11, 11:11 am, "JohnQ" <[email protected]>
wrote:
"Then they're not templates."
Not "C++ templates" you mean.

"Not templates. C++ didn't invent templates; they were present
in languages long before C++ added them. In fact, C++ added
them mainly because the earlier macro-based solution didn't
work."

You keep saying that, but of course it isn't true, for my containers and
algos work just fine. You're thinking about some pre-existing implementation
that had failed for any of a number of reasons, not the least of which is
that if you define "successful" as being all things to all people all of the
time, then something that doesn't include the kitchen sink is labeled a
failure.
"Basically, templates were added to
the language in CFront 3.0 because macros (<generic.h>) were
found to be insufficient."

"Whatever. The fact remains that a macro based solution was
tried, over a long period of time, by many people of many
different persuations, and was found not to be workable."

Seems to work just fine for me.

"So the
authors of C++ looked around, and saw that the template based
solution in Ada and Eiffel did work, and "invented" something
similar. Not identical, because of course Ada and Eiffel are
different languages, with a different philosophy, and their
version of templates wouldn't fit in nicely with that of C++.
But the fact remains that templates were added to C++ because
they worked well in other languages, and the pre-processor based
solution which was present before didn't work."

It depends on your requirements of the concept. If you like implementing
technologies which give "10% more functionality for 90% more complexity",
power to ya. I'll stick with the 80% solution that is simple and elegant.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top