|
Boost : |
From: mfdylan (dylan_at_[hidden])
Date: 2001-12-21 01:36:36
I mentioned this in a message recently, and I actually think this
idea has quite a lot of potential. Basically what I suggested is a
method for specifying a message format with positional parameters
that the compiler can parse. That is, the format specification
itself constructs some sort of formatting object, that when
parameters are inserted into it constructs an object holding the
parameters, that when inserted into a stream unravels itself to
insert all the parameters and associated "in between" bits of text
into the stream in the right order.
I suggested something like
(P1 + " found " + P2 + " times in " + P3)
where P1, P2, P3 were of type param<1>, param<2>, param<3>.
operator + is then overloaded to produce a formatting object, which
would be a kind of typelist. One thing I worked out pretty quickly
is that it would be must easier to construct it backwards, basically
because typelists are always easier to prepend to than append to.
So the above might produce a
formatter<3, formatter<2, formatter<1, endf> > >
holding the bits of text in between parameters.
Inserting parameters into this would generate a more complex type-
list holding the bits of text and the parameters.
The tricky part is determining how to reorder the parameters.
The idea I have is for each parameter insertion to produce a new type
with an incremented "position" parameter, and then basically you walk
the resulting typelist, outputting to the stream every time the
actual position matches the requested position. This would still
allow someone to specify, for instance:
(P1 + " found in " + P3 + ". Select " + P1 + "?")
where P1 is used twice, and P2 isn't even used (conceivably in
certain languages it may not be possible to sensibly incorporate all
parameters into a message).
The only ugly thing I can see is that you'll basically have to
specify the messages via macros:
#define msg_match_found (P1 + " found " + P2 + " times in " + P3)
#define msg_file_not_found (P1 + " not found in " + P2)
etc.
because it would be unreasonable (and unnecessary) for the user to
work out the resulting type.
Unfortunately my experience with this sort mega-template-heavy meta-
programming is all but zero, and I was thwarted in getting very far
by VC anyway. Anyone think this is a challenge worth taking up?
Dylan
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk