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 6918517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:52:10+00:00 2026-05-27T09:52:10+00:00

I’ve got a nasty problem with some code and can’t see the error. I

  • 0

I’ve got a nasty problem with some code and can’t see the error. I have a template function

template<typename T, typename F> void RecursiveParseLeftAssociativeBinaryExpression(std::unique_ptr<Wide::Parser::ExpressionAST>& cur_expr, F f) {
    CheckedIncrement();
    auto new_expr = Wide::make_unique<T>();
    new_expr->lhs = std::move(cur_expr);
    new_expr->rhs = f();
    cur_expr = std::move(new_expr);
}

with the obvious implementation of make_unique. There are other overloads, but they differ in obvious ways like different parameter numbers, so I know that this is the only one being called. Then I call it, like so (this is the error line, 315):

RecursiveParseLeftAssociativeBinaryExpression<Wide::Parser::AccessExpression>(current_expression, [&] { return this_ptr->RecursiveParseExpression(); });

But the compiler insists that I am using a type as an expression. I’m pretty sure that this code is completely legal. Am I missing something? The error is

1>parser.cpp(315): error C2275: 'Wide::Parser::AccessExpression' : illegal use of this type as an expression

Edit: These are the definitions of the other overloads.

template<typename First, Wide::Lexer::TokenType first, typename F> std::unique_ptr<Wide::Parser::ExpressionAST> RecursiveParseLeftAssociativeBinaryExpression(F next) {
    auto cur_expr = next();
    std::function<void()> RecursiveParseSingleToken;
    RecursiveParseSingleToken = [&] {
        switch(begin->type) {
        case first:
            RecursiveParseLeftAssociativeBinaryExpression<First>(cur_expr, next);
            return RecursiveParseSingleToken();
        }
    };
    RecursiveParseSingleToken();
    return std::move(cur_expr);
}

template<typename First, typename Second, Wide::Lexer::TokenType first, Wide::Lexer::TokenType second, typename F> std::unique_ptr<Wide::Parser::ExpressionAST>
RecursiveParseLeftAssociativeBinaryExpression(F next) {
    auto cur_expr = next();
    std::function<void()> RecursiveParseDualToken;
    RecursiveParseDualToken = [&] {
        switch(begin->type) {
        case first:
            RecursiveParseLeftAssociativeBinaryExpression<First>(cur_expr, next);
            return RecursiveParseDualToken();
        case second:
            RecursiveParseLeftAssociativeBinaryExpression<Second>(cur_expr, next);
            return RecursiveParseDualToken();
        }
    };
    RecursiveParseDualToken();

    return std::move(cur_expr);
}

However, they clearly differ in requiring additional explicit template arguments, and so cannot be the call resolved.

Edit: The definition of make_unique:

namespace Wide {
    template<typename T> std::unique_ptr<T> make_unique() {
        return std::unique_ptr<T>(new T);
    }
};

Just to be certain, the calling code isn’t in any template of any kind, and there are absolutely no dependent names. Wide is a namespace.

Another edit: The error is repeated for all the direct calls to this overload, but not for the ones which are called through the other overloads, curiously.

Edit: This 100line test case reproduces the problem.

#include <memory>
#include <functional>
#include <vector>

namespace Wide {
    template<typename T> std::unique_ptr<T> make_unique() {
        return std::unique_ptr<T>(new T);
    }
    class Lexer {
    public:
        enum TokenType {
            Namespace,
            For,
            Identifier
        };
        struct Token {
            TokenType type;
        };
    };
    class Parser {
    public:
        struct ExpressionAST {};
        struct BinaryExpressionAST : public ExpressionAST {
            std::unique_ptr<ExpressionAST> lhs;
            std::unique_ptr<ExpressionAST> rhs;
        };
        struct AccessExpression : public BinaryExpressionAST {};
        struct IdentifierExpression : public ExpressionAST {};
    };
}

typedef std::vector<Wide::Lexer::Token>::iterator Iterator;

struct inner_parser {
    Iterator begin, end;

    std::unique_ptr<Wide::Parser::ExpressionAST> RecursiveParsePrimaryExpression() {
        if (begin->type == Wide::Lexer::TokenType::Identifier) {
            CheckedIncrement();
            return Wide::make_unique<Wide::Parser::IdentifierExpression>();
        }
        throw std::runtime_error("aah");
    }
    template<typename T, typename F> void RecursiveParseLeftAssociativeBinaryExpression(std::unique_ptr<Wide::Parser::ExpressionAST>& cur_expr, F f) {
        CheckedIncrement();
        auto new_expr = Wide::make_unique<T>();
        new_expr->lhs = std::move(cur_expr);
        new_expr->rhs = f();
        cur_expr = std::move(new_expr);
    }
    template<typename First, Wide::Lexer::TokenType first, typename F> std::unique_ptr<Wide::Parser::ExpressionAST>
    RecursiveParseLeftAssociativeBinaryExpression(F next) {
        auto cur_expr = next();
        std::function<void()> RecursiveParseSingleToken;
        RecursiveParseSingleToken = [&] {
            switch(begin->type) {
            case first:
                RecursiveParseLeftAssociativeBinaryExpression<First>(cur_expr, next);
                return RecursiveParseSingleToken();
            }
        };
        RecursiveParseSingleToken();
        return std::move(cur_expr);
    }

    template<typename First, typename Second, Wide::Lexer::TokenType first, Wide::Lexer::TokenType second, typename F>
    std::unique_ptr<Wide::Parser::ExpressionAST>
        RecursiveParseLeftAssociativeBinaryExpression(F next) {
            auto cur_expr = next();
            std::function<void()> RecursiveParseDualToken;
            RecursiveParseDualToken = [&] {
                switch(begin->type) {
                case first:
                    RecursiveParseLeftAssociativeBinaryExpression<First>(cur_expr, next);
                    return RecursiveParseDualToken();
                case second:
                    RecursiveParseLeftAssociativeBinaryExpression<Second>(cur_expr, next);
                    return RecursiveParseDualToken();
                }
            };
            RecursiveParseDualToken();

            return std::move(cur_expr);
    }

    void CheckedIncrement() {
        if (begin == end)
            throw std::runtime_error("aaaaaah!");
        begin++;
    }
};
int main() {
    std::vector<Wide::Lexer::Token> tokens;
    inner_parser ip;
    ip.begin = tokens.begin();
    ip.end = tokens.end();
    auto ret = ip.RecursiveParseLeftAssociativeBinaryExpression<Wide::Parser::AccessExpression, Wide::Lexer::TokenType::For>([&] { return ip.RecursiveParsePrimaryExpression(); });
    return 0;
}
  • 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-05-27T09:52:10+00:00Added an answer on May 27, 2026 at 9:52 am

    Sprinkling this-> liberally inside of the template member functions’ lambdas seems to fix your test case:

    #include <memory>
    #include <functional>
    #include <vector>
    
    namespace Wide {
        template<typename T> std::unique_ptr<T> make_unique() {
            return std::unique_ptr<T>(new T);
        }
        class Lexer {
        public:
            enum TokenType {
                Namespace,
                For,
                Identifier
            };
            struct Token {
                TokenType type;
            };
        };
        class Parser {
        public:
            struct ExpressionAST {};
            struct BinaryExpressionAST : public ExpressionAST {
                std::unique_ptr<ExpressionAST> lhs;
                std::unique_ptr<ExpressionAST> rhs;
            };
            struct AccessExpression : public BinaryExpressionAST {};
            struct IdentifierExpression : public ExpressionAST {};
        };
    }
    
    typedef std::vector<Wide::Lexer::Token>::iterator Iterator;
    
    struct inner_parser {
        Iterator begin, end;
    
        std::unique_ptr<Wide::Parser::ExpressionAST> RecursiveParsePrimaryExpression() {
            if (begin->type == Wide::Lexer::Identifier) {
                CheckedIncrement();
                return Wide::make_unique<Wide::Parser::IdentifierExpression>();
            }
            throw std::runtime_error("aah");
        }
        template<typename T, typename F> void RecursiveParseLeftAssociativeBinaryExpression(std::unique_ptr<Wide::Parser::ExpressionAST>& cur_expr, F f) {
            CheckedIncrement();
            auto new_expr = Wide::make_unique<T>();
            new_expr->lhs = std::move(cur_expr);
            new_expr->rhs = f();
            cur_expr = std::move(new_expr);
        }
        template<typename First, Wide::Lexer::TokenType first, typename F> std::unique_ptr<Wide::Parser::ExpressionAST>
        RecursiveParseLeftAssociativeBinaryExpression(F next) {
            auto cur_expr = next();
            std::function<void()> RecursiveParseSingleToken;
            RecursiveParseSingleToken = [&] {
                switch(this->begin->type) {
                case first:
                    this->RecursiveParseLeftAssociativeBinaryExpression<First>(cur_expr, next);
                    return RecursiveParseSingleToken();
                }
            };
            RecursiveParseSingleToken();
            return std::move(cur_expr);
        }
    
        template<typename First, typename Second, Wide::Lexer::TokenType first, Wide::Lexer::TokenType second, typename F>
        std::unique_ptr<Wide::Parser::ExpressionAST>
            RecursiveParseLeftAssociativeBinaryExpression(F next) {
                auto cur_expr = next();
                std::function<void()> RecursiveParseDualToken;
                RecursiveParseDualToken = [&] {
                    switch(this->begin->type) {
                    case first:
                        this->RecursiveParseLeftAssociativeBinaryExpression<First>(cur_expr, next);
                        return RecursiveParseDualToken();
                    case second:
                        this->RecursiveParseLeftAssociativeBinaryExpression<Second>(cur_expr, next);
                        return RecursiveParseDualToken();
                    }
                };
                RecursiveParseDualToken();
    
                return std::move(cur_expr);
        }
    
        void CheckedIncrement() {
            if (begin == end)
                throw std::runtime_error("aaaaaah!");
            begin++;
        }
    };
    int main() {
        std::vector<Wide::Lexer::Token> tokens;
        inner_parser ip;
        ip.begin = tokens.begin();
        ip.end = tokens.end();
        auto ret = ip.RecursiveParseLeftAssociativeBinaryExpression<Wide::Parser::AccessExpression, Wide::Lexer::For>([&] { return ip.RecursiveParsePrimaryExpression(); });
        return 0;
    }
    

    I’d speculate that this is a bug in VC++ 2010’s lambda implementation, wherein class-scope name resolution is not performed properly for member templates.

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

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

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.