MSVC10中的奇怪编译器错误

时间:2011-11-07 22:05:37

标签: c++ visual-studio-2010 c++11

我有以下代码:

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();
    }
};

不,真的。现在它编译并运行得很好。

2 个答案:

答案 0 :(得分:1)

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(), 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 :(得分:1)

我现在遇到同样的问题。我使用其他类型,但对于你的情况,它将是这样的:

auto end_current_token = [&] {

    using Wide::Lexer::TokenType;           // <-- this line solves problem

    if (current != Wide::Lexer::Token()) {
        current.type = Wide::Lexer::TokenType::Identifier;

现在它汇编得很好。

相关问题