Boost logo

Boost :

From: Matt Hurd (matt.hurd_at_[hidden])
Date: 2005-04-11 01:13:18

On Apr 8, 2005 9:49 PM, Hartmut Kaiser <HartmutKaiser_at_[hidden]> wrote:
> Matt Hurd wrote:
> > Do you think Wave could be used for adding some syntatic
> > sugar that would allow easy compile time programming of strings?
> What concretely do you have in mind?

I've been toying with some thoughts of writing some compile time
parsing functionality. To do this I need compile time strings which
aren't in the standard. I think the closest thing is probably using a
mpl::vector_c< char...

The definitions of such are a bit unwieldy to say the least. E.g.
    typedef mpl::vector_c< char, 'h', 'e', 'l', 'l', 'o' > cts_hello;
    typedef mpl::vector_c< char, 'w', 'o', 'r', 'l', 'd' > cts_world;

I could localise such things in a resource file and have a little
program transform it into an appropriate hpp or perhaps use a little
preprocessor extension to do the same if it was straightforward.

For example, a quick hack of a compile time string doo-dad:

#include <iostream>
#include <string>

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/back_inserter.hpp>
#include <boost/mpl/pop_front.hpp>

namespace mpl = boost::mpl;

typedef mpl::vector_c< char, 'h', 'e', 'l', 'l', 'o' > cts_hello;
typedef mpl::vector_c< char, ' ', 'w', 'o', 'r', 'l', 'd' > cts_world;

template < class CTS >
struct cts_to_rts
    char value[ mpl::size< CTS >::value ] ;

        write_string< mpl::size< CTS >::value, CTS >::apply( value );

    std::string string_copy()
        return std::string( value, mpl::size< CTS >::value );

    template< int N, class CTS >
    struct write_string
        static void apply( char * p )
            *p = mpl::front< CTS >::type::value;
            write_string<N-1, typename mpl::pop_front< CTS >::type
>::apply( ++p );

    template< class CTS >
    struct write_string< 0, typename CTS >
        static void apply( char * p )



typedef mpl::copy
    , mpl::back_inserter< cts_hello >

int main()
    cts_to_rts< cts_hello_world > rts_hello_world;

    std::cout << rts_hello_world.string_copy() << "\n";
    return 0;

> I'm not quite sure if it is sufficient to have Wave around to solve the
> problem you've mentioned. Probably you'll need some kind of C++ semantic
> analysis for that (i.e. a parser for C++ or a significant subset).

was thinking perhaps a

could be translated to
    typedef mpl::vector_c< char, 'h', 'e', 'l', 'l', 'o' > cts_hello

by wave or something more appropriate. Simple enough to do my own
preprocessor pass for this as it is a fairly straight forward

Just wondering out loud as I can imagine all sorts of nice regex,
parsing and state machine things I'd like to be able to do at compile

Any better thoughts on pseudo string manipulation at compile time?


Boost list run by bdawes at, gregod at, cpdaniel at, john at