473,703 Members | 3,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Are C++ templates a precompiler thing?

Well apparently not since one can step thru template code with a debugger.
But if I was willing to make the concession on debugging, templates would be
strictly a precompiler thing? I have a feeling the answer I'm going to get
back will be "no, because templates have taken on a life of their own since
their original conception and now also affect compiler implementation"
(read: not good, IMO.

John
Jun 11 '07
104 4570

"Gianni Mariani" <gi*******@mari ani.wswrote in message
news:46******** *************** @per-qv1-newsreader-01.iinet.net.au ...
JohnQ wrote:
...
>I'm not convinced that is true. Are you saying that macros can't be
type-safe?

Macros can't be chosen by type of parameter line a function can.
I'm not sure what you mean. Show me.

John
Jun 12 '07 #11

"Dizzy" <di***@roedu.ne twrote in message news:f4******** **@aioe.org...
JohnQ wrote:
>>
Can they be implemented that way though (early work did exactly that).

Yes, _earlier_. But also back in that time they had less semantics that
ISO
C++ templates have.
That's a good thing if you ask me though: "keeping them on a leash".
>
>>
>>If you want to use preprocessor macros
(and there's no reason not to), use "#define", not "template".

What's the diff?

For one you write "#define" and for the other you write "template". Or be
more specific about your question.
Template functionality (not the complex C++ incarnation of them) can be
implemented in various ways.
>
>>I, for one, love macros. And templates. I use both all the
time. But they're not even remotely similar.

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

Any reasons why "they should be the same"?
Reduction of complexity.
There are a lot of semantic
differences ATM. templates are dealt with at compile time by the compiler
which is after the preprocessor has ran. Much of their semantics depend on
compiler only knowledge (such as types to deduce function template
parameters, evaluation of constant expressions at compile time, SFINAE
functionality, template names are part of the C++ compile time name system
thus working with the rules of Koening lookup and the namespace
composition
and importing semantics, etc).

How would you implement something like SFINAE in the preprocessor?
Maybe I don't need that.
>
>Again, I whole-heartedly disagree. (Yeah, I've implemented those kinds of
things).

I seriously doubt your implementation supports SFINAE, ADL and template
parameter deduction for function templates.
I don't think I need that.

John
Jun 12 '07 #12

"Gianni Mariani" <gi*******@mari ani.wswrote in message
news:46******** *************** @per-qv1-newsreader-01.iinet.net.au ...
JohnQ wrote:
>"Robbie Hatley" <bo***********@ no.spamwrote in message
news:Yh******* ************@fe 02.news.easynew s.com...
>>"JohnQ" wrote:

Are C++ templates a precompiler thing?
By "precompile r", I presume you mean the C/C++ preprocessor?
That's not *any* kind of "compiler", pre-, post-, or otherwise.
It's just a text editor.

Templates are a part of the C++ language. They are not
preprocesso r macros.

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

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++.
>
>>
>>If you want to use preprocessor macros
(and there's no reason not to), use "#define", not "template".

What's the diff?

This is a cute piece of code - do this with macros.
[example omitted}

I don't have a need to do that though. If you need to, then yeah, you may
wanna use the highly complex C++ templates.
Can you create a macro to tell you if a type converts to another type or
if a type has a particular member or change behaviour of a function
template for a specific type (I call this the C++ come-from).

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, for one, love macros. And templates. I use both all the
time. But they're not even remotely similar.

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

Templates do have a learning curve. You do have to think of your code in
a more generic fashion.
Just containers thank you.
>
>>
...
>>
Again, I whole-heartedly disagree.

That you disagree is great. Don't stop there, do tell us of your
reasoning.
I limit what templates can do by requiring of them that they can be
implemented simply.

John
Jun 12 '07 #13

"Ian Collins" <ia******@hotma il.comwrote in message
news:5d******** ******@mid.indi vidual.net...
JohnQ wrote:
>"Ian Collins" <ia******@hotma il.comwrote:
>>such as file only scope and lack of type safety.

I'm not convinced that is true. Are you saying that macros can't be
type-safe?
How can they be? They are simple text substitutions.
But the text can expand to type-safe code (the same as C++ templates do).
You'll get an error at compile time if you try to pass a wrong type into a
function, for example. I don't see any difference whether templates are
implemented via the preprocessor or the compiler in regards to type safety.

John
Jun 12 '07 #14
JohnQ wrote:
"Gianni Mariani" <gi*******@mari ani.wswrote in message
news:46******** *************** @per-qv1-newsreader-01.iinet.net.au ...
>JohnQ wrote:
...
>>I'm not convinced that is true. Are you saying that macros can't be
type-safe?
Macros can't be chosen by type of parameter line a function can.

I'm not sure what you mean. Show me.
void write( int a )
{
ptintf( "%dZ", a );
}

void write( const char * str )
{
printf( "%s", str );
}
#define write( A ) printf( ???? );

Jun 12 '07 #15
JohnQ wrote:
"Ian Collins" <ia******@hotma il.comwrote in message
news:5d******** ******@mid.indi vidual.net...
>JohnQ wrote:
>>"Ian Collins" <ia******@hotma il.comwrote:

such as file only scope and lack of type safety.
I'm not convinced that is true. Are you saying that macros can't be
type-safe?
How can they be? They are simple text substitutions.

But the text can expand to type-safe code (the same as C++ templates do).
You'll get an error at compile time if you try to pass a wrong type into a
function, for example. I don't see any difference whether templates are
implemented via the preprocessor or the compiler in regards to type safety.
OK, show us a map container implemented as macros.

--
Ian Collins.
Jun 12 '07 #16
JohnQ wrote:
....
>
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!
It's just an example of a problem that has been solved many times and it
degenerates into alot of tedious code. This is a case where templates
simplify the code dramatically, if you know how to use them. The code
in this case is a simple transformation from the specification.
>
>>>I, for one, love macros. And templates. I use both all the
time. But they're not even remotely similar.
I disagree. I'm leaning toward "they should be the same". Beyond that
(macros), templates become unwieldly.
Templates do have a learning curve. You do have to think of your code in
a more generic fashion.

Just containers thank you.
Containers now, then smart pointers, special iterators etc etc. It's a
powerful tool if you learn to use it.
>
>>
...
>>Again, I whole-heartedly disagree.
That you disagree is great. Don't stop there, do tell us of your
reasoning.

I limit what templates can do by requiring of them that they can be
implemented simply.
That's a "reason" ?
Jun 12 '07 #17

"Ian Collins" <ia******@hotma il.comwrote in message
news:5d******** ******@mid.indi vidual.net...
JohnQ wrote:
>"Ian Collins" <ia******@hotma il.comwrote in message
news:5d******* *******@mid.ind ividual.net...
>>JohnQ wrote:
"Ian Collins" <ia******@hotma il.comwrote:

such as file only scope and lack of type safety.
I'm not convinced that is true. Are you saying that macros can't be
type-safe?

How can they be? They are simple text substitutions.

But the text can expand to type-safe code (the same as C++ templates do).
You'll get an error at compile time if you try to pass a wrong type into
a
function, for example. I don't see any difference whether templates are
implemented via the preprocessor or the compiler in regards to type
safety.
OK, show us a map container implemented as macros.
That's not necessary. I refer you to early template implementations that
used generic.h. Once the preprocessor substitutes the type text into the
"macro" ("macro" is a somewhat inappropriate usage of the term when using
the preprocessor this way), a type-safe class exists and that is what gets
compiled. I think you are probably thinking about implementations that are
not templates (of any kind) but rather constructs based upon
pointers-to-void* ("C-style" containers/algos).

John
Jun 12 '07 #18
JohnQ wrote:
"Ian Collins" <ia******@hotma il.comwrote in message
news:5d******** ******@mid.indi vidual.net...
>JohnQ wrote:
>>"Ian Collins" <ia******@hotma il.comwrote in message
news:5d****** ********@mid.in dividual.net...
JohnQ wrote:
"Ian Collins" <ia******@hotma il.comwrote:
>
>such as file only scope and lack of type safety.
I'm not convinced that is true. Are you saying that macros can't be
type-safe?
>
How can they be? They are simple text substitutions.
But the text can expand to type-safe code (the same as C++ templates do).
You'll get an error at compile time if you try to pass a wrong type into
a
function, for example. I don't see any difference whether templates are
implemented via the preprocessor or the compiler in regards to type
safety.
OK, show us a map container implemented as macros.

That's not necessary. I refer you to early template implementations that
used generic.h. Once the preprocessor substitutes the type text into the
"macro" ("macro" is a somewhat inappropriate usage of the term when using
the preprocessor this way), a type-safe class exists and that is what gets
compiled. I think you are probably thinking about implementations that are
not templates (of any kind) but rather constructs based upon
pointers-to-void* ("C-style" containers/algos).
I ask again, show us a map container implemented with macros, time to
put up or shut up.

--
Ian Collins.
Jun 12 '07 #19

"Ian Collins" <ia******@hotma il.comwrote in message
news:5d******** *****@mid.indiv idual.net...
JohnQ wrote:
>"Ian Collins" <ia******@hotma il.comwrote in message
news:5d******* *******@mid.ind ividual.net...
>>JohnQ wrote:
"Ian Collins" <ia******@hotma il.comwrote in message
news:5d***** *********@mid.i ndividual.net.. .
JohnQ wrote:
>"Ian Collins" <ia******@hotma il.comwrote:
>>
>>such as file only scope and lack of type safety.
>I'm not convinced that is true. Are you saying that macros can't be
>type-safe?
>>
How can they be? They are simple text substitutions.
But the text can expand to type-safe code (the same as C++ templates
do).
You'll get an error at compile time if you try to pass a wrong type
into
a
function, for example. I don't see any difference whether templates are
implemente d via the preprocessor or the compiler in regards to type
safety.

OK, show us a map container implemented as macros.

That's not necessary. I refer you to early template implementations that
used generic.h. Once the preprocessor substitutes the type text into the
"macro" ("macro" is a somewhat inappropriate usage of the term when using
the preprocessor this way), a type-safe class exists and that is what
gets
compiled. I think you are probably thinking about implementations that
are
not templates (of any kind) but rather constructs based upon
pointers-to-void* ("C-style" containers/algos).
I ask again, show us a map container implemented with macros, time to
put up or shut up.
There's another post I just made that shows the concept using an list link
(note that it is not actual code from any library that I use, but is rather
just for illustrative purposes). None of this stuff is "top secret" BTW.
Though I can't post my actual container code here, you should be able to see
the concept quite clearly. Again, it's "old hat" stuff if you've been a C++
user from the beginning. See one of my latest posts in this thread for the
"pattern".

John
Jun 12 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.