C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Imagine you could pass a code block to a template parameter

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
christopher diggins
Guest





PostPosted: Wed Sep 22, 2004 10:42 pm    Post subject: Imagine you could pass a code block to a template parameter Reply with quote



Imagine if you could pass a code block (i.e. { to } ) as a template argument
.... and then expand it inline using a special operator, say # for the sake
of argument. Then you could things like this:

template<bool b, typename T1, typename T2> ConcatenateBlock() {
#T1
#T2
};

void FuBar() {
#ConcatenateBlock
<
{cout << "fu"; << endl; },
{cout << "bar"; << endl; }
Quote:

}


But that is only the tip of the iceberg. It would allow for much more
sophisticated metaprogramming than is currently possible. How much value can
people see in allowing such a construct for C++?

On a related note, If anyone is interested, I have built this capacity into
Heron and I explain it in depth at
http://www.heron-language.com/metaprogramming.html

--
Christopher Diggins

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
David Abrahams
Guest





PostPosted: Thu Sep 23, 2004 4:45 pm    Post subject: Re: Imagine you could pass a code block to a template parame Reply with quote



[email]cdiggins (AT) videotron (DOT) ca[/email] ("christopher diggins") writes:

Quote:
Imagine if you could pass a code block (i.e. { to } ) as a template argument
... and then expand it inline using a special operator, say # for the sake
of argument. Then you could things like this:

template<bool b, typename T1, typename T2> ConcatenateBlock() {
#T1
#T2
};

void FuBar() {
#ConcatenateBlock

{cout << "fu"; << endl; },
{cout << "bar"; << endl; }

}

You can already pass function pointers/references as template
parameters. If those functions are inline they can be expanded
inline. All we'd need is true lambdas and we'd be able to do
everything you're doing above.

Quote:
But that is only the tip of the iceberg. It would allow for much more
sophisticated metaprogramming than is currently possible. How much value can
people see in allowing such a construct for C++?

On a related note, If anyone is interested, I have built this capacity into
Heron and I explain it in depth at
http://www.heron-language.com/metaprogramming.html

That's just a fancy way of saying "macro" as far as I can tell Wink.
The block is preserved as raw text and only parsed in the context of
the function where it is generated, right?

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
christopher diggins
Guest





PostPosted: Thu Sep 23, 2004 6:45 pm    Post subject: Re: Imagine you could pass a code block to a template parame Reply with quote



Hi Dave,

"David Abrahams" <dave (AT) boost-consulting (DOT) com> wrote

Quote:
cdiggins (AT) videotron (DOT) ca ("christopher diggins") writes:

Imagine if you could pass a code block (i.e. { to } ) as a template
argument
... and then expand it inline using a special operator, say # for the
sake
of argument. Then you could things like this:

template<bool b, typename T1, typename T2> ConcatenateBlock() {
#T1
#T2
};

void FuBar() {
#ConcatenateBlock

{cout << "fu"; << endl; },
{cout << "bar"; << endl; }

}

You can already pass function pointers/references as template
parameters. If those functions are inline they can be expanded
inline. All we'd need is true lambdas and we'd be able to do
everything you're doing above.

A hypothetical true lambda would undoubtably be beneficial. The code-block
propopsal would have access to variables local to the scope of where the
expansion occurs (like a macro).

Quote:
But that is only the tip of the iceberg. It would allow for much more
sophisticated metaprogramming than is currently possible. How much value
can
people see in allowing such a construct for C++?

On a related note, If anyone is interested, I have built this capacity
into
Heron and I explain it in depth at
http://www.heron-language.com/metaprogramming.html

That's just a fancy way of saying "macro" as far as I can tell Wink.

A macro is just text whereas a code-block is guaranteed to be a set of well
formed statements contained within matching curly braces. Macros can not be
passed as a template parameter.

Quote:
The block is preserved as raw text and only parsed in the context of
the function where it is generated, right?

Yes. But when a code-block is declared as a meta-function, the parameters
are guaranteed to be types as opposed to just primitive token replacement
like a macro. This allows us to completely avoid the macro token replacement
error problems. In the end Heron metaprogramming replaces the need for
macros by more fully leveraging the type system. This is what could be done
in C++ if you allowed blocks to be treated like types.

--
Christopher Diggins
http://www.heron-language.com

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.