c – MSVC10中的奇怪编译器错误

我有以下代码:

std::for_each(tokens.begin(),tokens.end(),[&](Token& t) {
    static const std::unordered_map<std::wstring,Wide::Lexer::TokenType> mapping([]() -> std::unordered_map<std::wstring,Wide::Lexer::TokenType>
    {
        // Maps strings to TokenType enumerated values
        std::unordered_map<std::wstring,Wide::Lexer::TokenType> result;

        // RESERVED WORD
        result[L"namespace"] = Wide::Lexer::TokenType::Namespace;
        result[L"for"] = Wide::Lexer::TokenType::For;
        result[L"while"] = Wide::Lexer::TokenType::While;
        result[L"do"] = Wide::Lexer::TokenType::Do;
        result[L"type"] = Wide::Lexer::TokenType::Type;

        // PUNCTUATION
        result[L"{"] = Wide::Lexer::TokenType::OpenCurlyBracket;
        result[L"}"] = Wide::Lexer::TokenType::CloseCurlyBacket;
        return result;
    }());
    if (mapping.find(t.Codepoints) != mapping.end()) {
        t.type = mapping.find(t.Codepoints)->second;
        return;
    }
    t.type = Wide::Lexer::TokenType::Identifier; // line 121
});

这将遍历一个令牌列表,并根据代码点的内容进行判断,从相关的枚举中为它们分配一个值.如果找不到,则给它一个“标识符”值.但这无法编译.

1>Lexer.cpp(121): error C2065: '__this' : undeclared identifier
1>Lexer.cpp(121): error C2227: left of '->Identifier' must point to class/struct/union/generic type

这是完整错误,没有警告,没有其他错误.什么?我该如何解决这个错误?

编辑:我做了一些重要的重构,我在一个更简单的lambda中得到了完全相同的问题.

auto end_current_token = [&] {
    if (current != Wide::Lexer::Token()) {

        current.type = Wide::Lexer::TokenType::Identifier; // error line

        if (reserved_words.find(current.Codepoints) != reserved_words.end())
            current.type = reserved_words.find(current.Codepoints)->second;
        if (punctuation.find(current.Codepoints[0]) != punctuation.end())
            current.type = punctuation.find(current.Codepoints[0])->second;

        tokens.push_back(current);
        current = Wide::Lexer::Token();
    }
};

我已经清理并重建了这个项目.

我解决了这个问题.

auto end_current_token = [&] {
    if (current != Wide::Lexer::Token()) {

        // WORKAROUND compiler bug- dead code
        struct bug_workaround_type {
            int Identifier;
        };
        bug_workaround_type bug;
        bug_workaround_type* __this = &bug;

        current.type = Wide::Lexer::TokenType::Identifier;

        if (reserved_words.find(current.Codepoints) != reserved_words.end())
            current.type = reserved_words.find(current.Codepoints)->second;
        if (punctuation.find(current.Codepoints[0]) != punctuation.end())
            current.type = punctuation.find(current.Codepoints[0])->second;

        tokens.push_back(current);
        current = Wide::Lexer::Token();
    }
};

不完全是.现在它编译并运行得很好.

解决方法

FWIW我试图编写一个最小的工作样本,以便在VS2010上编译并编译以下内容而不会出错.

#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>

namespace Wide { namespace Lexer {
    enum TokenType
    {
        OpenCurlyBracket,CloseCurlyBacket,Namespace,For,While,Do,Type,Identifier,};
} }

struct Token
{
    std::wstring Codepoints;
    Wide::Lexer::TokenType type;
};

int main()
{
    std::vector<Token> tokens;
    std::for_each(tokens.begin(),[&](Token& t) {
        static const std::unordered_map<std::wstring,Wide::Lexer::TokenType>
        {
            // Maps strings to TokenType enumerated values
            std::unordered_map<std::wstring,Wide::Lexer::TokenType> result;

            // RESERVED WORD
            result[L"namespace"] = Wide::Lexer::TokenType::Namespace;
            result[L"for"] = Wide::Lexer::TokenType::For;
            result[L"while"] = Wide::Lexer::TokenType::While;
            result[L"do"] = Wide::Lexer::TokenType::Do;
            result[L"type"] = Wide::Lexer::TokenType::Type;

            // PUNCTUATION
            result[L"{"] = Wide::Lexer::TokenType::OpenCurlyBracket;
            result[L"}"] = Wide::Lexer::TokenType::CloseCurlyBacket;
            return result;
        }());
        if (mapping.find(t.Codepoints) != mapping.end()) {
            t.type = mapping.find(t.Codepoints)->second;
            return;
        }
        t.type = Wide::Lexer::TokenType::Identifier; // line 121
    });
}

你可以从这段代码开始,将显示问题的最小编辑一分为二吗?

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...