Sign in to follow this  
doofer

Assigning complicated types and separating variables and temporaries

Recommended Posts

Hello everyone, To learn some more about templates, I'm making something similar to boost::spirit (yes, I'm reinventing the wheel, no need to tell me). Basically, this is a language parser generated at compile time by clever use of operator overloading and templates. The generated parser is a class composed of many different classes, and may be arbitrarily complex in its structure. Up until now, I've been using boost::function for assigning these parsers, for example:
typedef boost::function<bool (It& in, It& end)> Parser;
Parser num = Range('0','9');
Parser alphanum = Range('a','z') | num;

This has worked fine for me, and also allowed me to store objects of type Parser as references inside other parser compositions, to allow recursion, et c, using the following template:
template<typename T>
struct ParserHolder
{
	typedef T type;
};

template<>
struct ParserHolder<Parser>
{
	typedef Parser& type;
};

Now, however, I've run into problems using boost::function, as I need to overload the [] operator to be able to let a parser have an action assigned to it. The following code won't work, because num is a boost::function which does not let me use the [] operator of the underlying Range object (for obvious reasons).
typedef boost::function<bool (It& in, It& end)> Parser;
Parser num = Range('0','9');
Parser alphanum = Range('a','z') | num[num_action];

Now, I thought I'd use boost::typeof to be able to define variables of complicated, composed types without having to type it all out, which solves the operator[] problem. This, however, breaks my old Parserholder. My question: Is there any way to separate variables and temporaries (Range('a','z') and num in my example) using templates to make it function similarly to my ParserHolder type? Alternate question: Is there a library similar to boost::function that allows one to use not only the function of a function object, but other operators and functions? I've tried looking at the source of boost::spirit to see if I can make anything out of it, but it's much to complicated for me to grasp, and also doesn't function exactly like my parsers. Any help is greatly appreciated! Thanks in advance!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this