Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8229799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:55:04+00:00 2026-06-07T16:55:04+00:00

I have encountered a slightly unusual problem. Consider the following code: class parser {

  • 0

I have encountered a slightly unusual problem. Consider the following code:

class parser
{
    lexer lex;

public:

    node_ptr parse(const std::string& expression)
    {
        lex.init(expression.begin(), expression.end());
        // ...
        // call some helper methods
        // return the result
    }

private:

    // lots of small helper methods, many of them accessing lex
};

The parse method initializes the lexer with an init method. Before that, the lexer is in an unusable “default” state. Usually, one should intialize a member during construction, so why don’t I simply do this:

class parser
{
    lexer lex;

public:

    parser(const std::string& expr) : lex(expr.begin(), expr.end()) {}

    node_ptr parse()
    {
        // call some helper methods
        // return the result
    }
    // ...
};

First, this means a client can call the parse method multiple times, which wouldn’t make much sense.

Second, and more importantly, it can very easily cause serious lifetime issues:

parser my_parser("1 * 2 + 3 * 4");
auto root = my_parser.parse();

In the above code, the lexer will be initialized with a temporary string object that ceases to exist at the end of the line, so calling the parse method in the next line will invoke undefined behavior.

For both of these reasons, I really want to initialize and parse in the same method. Unfortunately, I cannot do it in the constructor, because I need to return a result, and constructors cannot return results.

Technically it is possible to construct the lexer inside the parse method and destruct it afterwards if I also change the constructor and the destructor accordingly:

class parser
{
    static std::string dummy;
    lexer lex;

public:

    parser() : lex(dummy.begin(), dummy.end())
    {
        lex.~lexer();
    }

    node_ptr parse(const std::string& expression)
    {
        new(&lex) lexer(expression.begin(), expression.end());
        // call some helper methods
        lex.~lexer();
        // return the result
    }

    ~parser()
    {
        new(&lex) lexer(dummy.begin(), dummy.end());
    }
    // ...
};

But this is by far the ugliest code I have written in a very long time. It’s also not exception-safe; what if a helper method throws? Indeed, that’s exactly what happens when a parse error is encountered.

So how should I solve this issue? Use a local lexer inside parse and have a lexer* member point to it? Use a boost::optional<lexer> member? Or should I just live with the init method? Or should I do the parsing in the constructor after all and throw an “expection” containing the desired result?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:55:05+00:00Added an answer on June 7, 2026 at 4:55 pm

    I would definitely not do your 2nd example. Better would be constructing the lexer in Parse(), and store a pointer or boost::optional. But if you want to allow this, then your helper functions must check whether the lexer is valid or not before continuing. Seems messy to me.

    Better yet is to just make Parse a standalone function. I envision this to be more sensible to callers, and solve your problem:

    void parser_helper(lexer& lex)
    {
        ...
    }
    
    node_ptr Parse(const std::string& inp)
    {
        lexer lex(inp);
        ...
        parser_helper(lex);
        ...
        return ret;
    }
    

    Or if you have more state to pass around…

    class parser_helper
    {
        lexer lex;
        ... other state here
    
    public:
        parser_helper(const std::string& inp) :
            lex(inp)
        {
        }
    
        ... helper functions here.
        void helper_function() { }
    }
    
    node_ptr Parse(const std::string& inp)
    {
        parser_helper helper(inp);
        ...
        helper.helper_function();
        ...
        return ret;
    }
    

    Either way, the lexer should just be an auto variable in the Parse function.

    The idea is that the interface the caller expects is just a single function. No need to make the caller deal with a class, just because the internals of Parse have state / helper functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have encountered the following code: class a { public: void * operator new(size_t
We have encountered a very strange class not found problem in our web app
I have encountered a wierd problem, I have a class that appears in the
I have encountered the following problem which proved to me that I know far
I have encountered what appears to be a bug with the YAML parser. Take
I have encountered a problem with creating a thumbnail from an uploaded image file,
I have encountered a weird problem, when i built my last ad-hoc version for
I have encountered a problem in one of my Java projects, which causes bugs.
I have encountered a most annoying problem that occurs on the PWD variable when
I have encountered a rather nasty problem with the DataGridView control (Windows.Forms, .NET Framework

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.