|
Boost : |
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2008-03-29 01:12:38
On Sat, Mar 29, 2008 at 4:40 AM, Steven Watanabe <watanabesj_at_[hidden]> wrote:
> AMDG
>
>
> Giovanni Piero Deretta wrote:
> > That is, something like this is actually possible:
> >
> > void foo () {
> > int i =
> > lambda
> > // introduce formal parameter names
> > [ _<class left>(),
> > _<class right>()
> > ]
> > // now use them!
> > [_<left>() + _<right>()]
> > // actual parameters values
> > ( 10, 20 );
> > }
> >
> > The _<...>() looks a bit clumsy, but it might be actually usable.
> >
>
> I'd rather not use _. arg sound better.
> lambda<class left, class right>(arg<left>() + arg<right>())
>
I've used _ to try to shorten the amount of redundant stuff as much as
possible,
but I agree that arg looks much better.
>
> > Still the non-descriptive _N placeholders bother me.
> > Using the named placeholder trick it would become:
> >
> > typedef compose<
> > // introduce formal args names
> > args<
> > class range
> > , class init
> > , class op
> > >
> > // expression
> > , fold(reverse(range), init, op)
> > // function name
> > > reverse_fold;
> >
> > With complex expressions, this could be quite a readability improvement.
> > Except for confirming that the above expression does actually compile
> > (at least with gcc), I have yet to try to implement it,
> > but I think it should be fairly easy.
> >
> > Comments
>
> Wow. This is awesome. You'd probably better reference the standard
> (3.3.1/5)
> since most people will look at this and be surprised to find that
> it's legal. (I certainly was)
>
Making compilers swallow this syntax won't be easy. I do not own the
standard (yes, I should be ashamed :( ),
but looking at the last c++0x standard, it seems that a forward
declaration in a template should belong to the nearest non class
namespace.
Comeau online reject this (rightly, it seems):
template<typename T>
struct expr;
void foo() {
typedef expr<class A> B;
}
because it inject class A inside foo namespace, and thus has static
linkage. This means that, unfortunately, if Comeau is right, the trick
can't be used in function scope.
GCC (both 4.2 and 4.3) incorrectly accepts it, because i think it puts
A in the global namespace.
This means that this fails with gcc:
enum A {};
namespace foo {
typedef expr<class A> B;
}
because it thinks that it is redeclaring the enum A as a class type.
Comeau accepts it.
-- gpd
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk