473,471 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Template metaprogramming = interpretation

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...
Jul 22 '05 #1
12 2126
"Dave" <be***********@yahoo.com> wrote...
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
Jul 22 '05 #2

"Dave" <be***********@yahoo.com> wrote in message news:vu************@news.supernews.com...
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.

Jul 22 '05 #3

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:u8%Fb.119086$8y1.371001@attbi_s52...
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.
Jul 22 '05 #4
Ron Natalie wrote:
"Dave" <be***********@yahoo.com> wrote in message news:vu************@news.supernews.com...
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 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 Natalie wrote:
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

Jul 22 '05 #5

"Jeff Schwab" <je******@comcast.net> wrote in message
news:G7********************@comcast.com...
Ron Natalie wrote:
"Dave" <be***********@yahoo.com> wrote in message news:vu************@news.supernews.com...
Would people agree with the statement that to a large degree, using templatemetaprogramming techniques turns a C++ compiler into a C++ interpreter (butjust for the metaprogrammed portions of the code)? It's not a perfect
analogy, but it seems to be a reasonable statement...
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!
Ron Natalie wrote:
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

Jul 22 '05 #6
Dave wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:G7********************@comcast.com...
Ron Natalie wrote:
"Dave" <be***********@yahoo.com> wrote in message
just for the metaprogrammed portions of the code)? It's not a perfect
analogy, but it seems to be a reasonable statement...


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!


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.

Jul 22 '05 #7

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bs********@dispatch.concentric.net...
Dave wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:G7********************@comcast.com...
Ron Natalie wrote:

"Dave" <be***********@yahoo.com> wrote in message
>just for the metaprogrammed portions of the code)? It's not a perfect
>analogy, but it seems to be a reasonable statement...

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!


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...
Jul 22 '05 #8
Gianni Mariani wrote:
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.

Jul 22 '05 #9
"Dave" <be***********@yahoo.com> wrote in message
news:vu************@news.supernews.com...
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?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #10
Mike Hewson wrote in news:bs************@ID-135553.news.uni-berlin.de:
"Dave" <be***********@yahoo.com> wrote in message
news:vu************@news.supernews.com...
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' ?


I put your query to google, I felt luky and got:

http://en2.wikipedia.org/wiki/Turing-complete
Is there an incomplete?


There is no non-sarcastic answer to this question.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #11

"Mike Hewson" <he******@austarnet.com.au> wrote in message
news:bs************@ID-135553.news.uni-berlin.de...
"Dave" <be***********@yahoo.com> wrote in message
news:vu************@news.supernews.com...
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?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal


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
Jul 22 '05 #12
"Dave" <be***********@yahoo.com> wrote in message
news:vu************@news.supernews.com...
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. :-)

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #13

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

Similar topics

0
by: Dave | last post by:
For those who might be so inclined, I was wondering if I might get honest critiques of my first real venture into template metaprogramming. This template metaprogram sorts a list of integers at...
5
by: Mohammad | last post by:
Hi, Is it possible to disable a method of a template class depending on the typename at compile time? thanks!
21
by: Protoman | last post by:
I've been looking at template metaprogramming. It seems really cool, make the compiler do most of the work. I have very simple program that uses TMP,it calculates the square of a number, but it...
5
by: Mark Stijnman | last post by:
I am trying to teach myself template metaprogramming and I have been trying to create lists of related types. I am however stuck when I want to make a template that gives me the last type in a...
7
by: Joe | last post by:
Hi, I found a concept named template metaprogramming that can be used in C+ + code at compile-time. I am a beginner at C++. But I am a programmer on the .NET platform. Do you know if template...
1
by: Ted | last post by:
I have cross posted this to comp.lang.c++ and to sci.math.num- analysis in the belief that the topic is of interest to some in both groups. I am building my toolkit, in support of my efforts in...
5
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.