Template metaprogramming = interpretation

D

Dave

Would people agree with the statement that to a large degree, using template
metaprogramming techniques turns a C++ compiler into a C++ interpreter (but
just for the metaprogrammed portions of the code)? It's not a perfect
analogy, but it seems to be a reasonable statement...
 
V

Victor Bazarov

Dave said:
Would people agree with the statement that to a large degree, using template
metaprogramming techniques turns a C++ compiler into a C++ interpreter (but
just for the metaprogrammed portions of the code)? It's not a perfect
analogy, but it seems to be a reasonable statement...

Every compiler is an interpreter if it can handle things like

int a = 1 + 2 + 3 + 4;

by creating a definition of 'a' initialised to 10 instead of
a special piece of code that performs three additions with the
immediate operands.

Victor
 
R

Ron Natalie

Dave said:
Would people agree with the statement that to a large degree, using template
metaprogramming techniques turns a C++ compiler into a C++ interpreter (but
just for the metaprogrammed portions of the code)? It's not a perfect
analogy, but it seems to be a reasonable statement...

I wouldn't agree with the statement. It's more of a supercompiler as it
can precompute code at compile time as opposed to translating language
at run time.
 
R

Ron Natalie

Victor Bazarov said:
Every compiler is an interpreter if it can handle things like

int a = 1 + 2 + 3 + 4;

by creating a definition of 'a' initialised to 10 instead of
a special piece of code that performs three additions with the
immediate operands.

Every compiler has to be able to handle that. 1 + 2 + 3 + 4
is a constant expression.

The fact that a compiler can precompute things at compile
time makes it LESS of an interpreter than more of one in my
opinion.
 
J

Jeff Schwab

I do see what you mean. You're getting the compiler to execute a
program for you, in much the same way that a Unix shell executes your
commands. However, the compiler is acting as a very special-purpose
interpreter, useful only for generating "traditional" code. The reason
is that run-time libraries, system interfaces, etc. are not available.

Ron said:
I wouldn't agree with the statement. It's more of a supercompiler as it
can precompute code at compile time as opposed to translating language
at run time.

I have to agree with Ron. The compiler is acting more like a CASE tool
than a traditional compiler. Speaking of which, whatever happened to
4GL's? Does C++ metaprogramming count?

-Jeff
 
D

Dave

Jeff Schwab said:
I do see what you mean. You're getting the compiler to execute a
program for you, in much the same way that a Unix shell executes your
commands. However, the compiler is acting as a very special-purpose
interpreter, useful only for generating "traditional" code. The reason
is that run-time libraries, system interfaces, etc. are not available.

Yeah, this characterization of template metaprogramming recently occurred to
me as I was trying my hand at a couple of miscellaneous problems just to see
if I could do it. I successfully implemented the compile-time calculation
of order statistics via template metaprogramming and it was at that point
the thought occurred to me that there is something sort of akin to
interpretation going on. Although, your point about the lack of
availability of any run-time libraries, etc... is well-taken!
 
G

Gianni Mariani

Dave said:
Ron said:
Yeah, this characterization of template metaprogramming recently occurred to
me as I was trying my hand at a couple of miscellaneous problems just to see
if I could do it. I successfully implemented the compile-time calculation
of order statistics via template metaprogramming and it was at that point
the thought occurred to me that there is something sort of akin to
interpretation going on. Although, your point about the lack of
availability of any run-time libraries, etc... is well-taken!

So the question is - do you want metaprogramming in the regular language?

I've written a number of fancy templates and I find having to think in
this recursve "COME FROM" language a little bit of a challenge.

Then if you start to argue about this too much you'll have someone
remind you that lisp does all this in one language.
 
D

Dave

Gianni Mariani said:
Dave said:
So the question is - do you want metaprogramming in the regular language?

I've written a number of fancy templates and I find having to think in
this recursve "COME FROM" language a little bit of a challenge.

Then if you start to argue about this too much you'll have someone
remind you that lisp does all this in one language.

Yep, I agree with your sentiments! Not to mention that syntactically it's
awful. But that's not the fault of the language since templates weren't
envisioned to be used in this manner. Nonetheless, I find the concept of
compile-time programming (and especially that it is Turing-complete)
fascinating...
 
J

Jeff Schwab

Gianni said:
So the question is - do you want metaprogramming in the regular language?

I've written a number of fancy templates and I find having to think in
this recursve "COME FROM" language a little bit of a challenge.

Then if you start to argue about this too much you'll have someone
remind you that lisp does all this in one language.

So do a number of other interpreted languages, including Perl and Tcl.
I think that's why template-based metaprogramming reminded the OP of an
interpreted language.
 
M

Mike Hewson

Dave said:
Nonetheless, I find the concept of compile-time programming
(and especially that it is Turing-complete) fascinating...

Well I thought I followed alot of this thread, but what's 'Turing-complete'
?
Is there an incomplete?
 
D

Dave

Mike Hewson said:
Well I thought I followed alot of this thread, but what's 'Turing-complete'
?
Is there an incomplete?

When something is "Turing-Complete", that means it is capable of computing
anything that is computable. And yep, there are things that are not
computable, ever, no matter how much processing power you have. Have you
ever heard of the halting problem? That is the classic example of something
that simply cannot be computed, period.

An example of something that is not Turing-Complete is a simple finite state
machine.

The term comes from Turing Machines, which are a well-known and simple
theoretical machine in Computer Science. Their elegance comes from the fact
that they can indeed compute anything that is computable, yet are very
simple machines.

In terms of this thread, template metaprogramming being Turing-Complete
means that you can compute anything that is comoutable using only template
metaprogramming techniques. There is nothing the full C++ language can do
computationally that can't be done just with template metaprogramming!
Although, programs that are purely metaprograms would be horrendous to read
and understand if they did anything non-trivial! Template metaprogramming
tends to get very ugly from a syntactic point of view...

Of course, as another poster pointed out, there is no I/O system built up
around template metaprogramming, so that makes it largely useless as a
general-purpose programming language (even if one were willing to put up
with the syntax), but that is a separate issue - that doesn't change the
fact that, computationally, template metaprogramming is capable of handling
any computable problem.

Another practical problem with template metaprogramming is that any
non-trivial metaprogram would likely involve very deep recursive template
instantiation, which would blow an internal limit in most compilers. But
again, this does not change the fact that, from a theoretical point of view,
template metaprogramming can compue anything that is computable (have I used
that phrase enough???!!! :) )...

Hope that clears it up!

Dave
 
M

Mike Hewson

Dave said:
When something is "Turing-Complete", that means it is capable of computing
anything that is computable. And yep, there are things that are not
computable, ever, no matter how much processing power you have. Have you
ever heard of the halting problem? That is the classic example of something
that simply cannot be computed, period.

An example of something that is not Turing-Complete is a simple finite state
machine.

The term comes from Turing Machines, which are a well-known and simple
theoretical machine in Computer Science. Their elegance comes from the fact
that they can indeed compute anything that is computable, yet are very
simple machines.

In terms of this thread, template metaprogramming being Turing-Complete
means that you can compute anything that is comoutable using only template
metaprogramming techniques. There is nothing the full C++ language can do
computationally that can't be done just with template metaprogramming!
Although, programs that are purely metaprograms would be horrendous to read
and understand if they did anything non-trivial! Template metaprogramming
tends to get very ugly from a syntactic point of view...

Of course, as another poster pointed out, there is no I/O system built up
around template metaprogramming, so that makes it largely useless as a
general-purpose programming language (even if one were willing to put up
with the syntax), but that is a separate issue - that doesn't change the
fact that, computationally, template metaprogramming is capable of handling
any computable problem.

Another practical problem with template metaprogramming is that any
non-trivial metaprogram would likely involve very deep recursive template
instantiation, which would blow an internal limit in most compilers. But
again, this does not change the fact that, from a theoretical point of view,
template metaprogramming can compue anything that is computable (have I used
that phrase enough???!!! :) )...

Hope that clears it up!

Dave

Outstanding, thank you!
Your explanation 'compute', 'compues', 'comouts' and probably 'comeaus' too.
:)
I'll have to go off and read "The Emperor's New Mind" again. :)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top