Jump to content
  • Advertisement
Sign in to follow this  

Assigning complicated types and separating variables and temporaries

This topic is 3712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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;

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!