首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法与默认构造的右值进行比较

无法与默认构造的右值进行比较
EN

Stack Overflow用户
提问于 2011-10-23 00:41:54
回答 1查看 117关注 0票数 1

我得到了以下简单的lambda:

代码语言:javascript
复制
auto end_current_token = [&] {
    if (current != Token()) {
        tokens.push_back(current);
        current = Token();
        cp = Codepoint();
    }
};

其中,current的类型为Token,并且提供了运算符。但是编译器给出了一个奇怪的错误:

代码语言:javascript
复制
1>Lexer.cpp(6): error C2273: 'function-style cast' : illegal as right side of '->' operator

这有什么问题呢?

编辑:尽管我想说这并不是所有相关的代码,但它确实是。除了在this上隐式使用之外,整个程序中没有一次使用->,错误消息清楚地指向所发布的lambda。但是,由于它是如此之小,我将发布所有的代码。

代码语言:javascript
复制
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
namespace Wide {
    class Lexer {
        struct Codepoint {
            Codepoint() {
                column = 0;
                line = 0;
                cp = 0;
            }
            int column;
            int line;
            wchar_t cp;
            bool operator==(wchar_t other) {
                return cp == other;
            }
        };
        enum TokenType {
            IDENTIFIER,
        };
        struct Token {
            Token()
                : line(0)
                , columnbegin(0)
                , columnend(0) {}
            Token(const Codepoint& cp) {
                *this = cp;
            }
            bool operator!=(const Token& other) {
                return !(line == other.line && columnbegin == other.columnbegin && columnend == other.columnend);
            }
            Token& operator+=(const Codepoint& cp) {
                if (cp.column >= columnend)
                    columnend = cp.column;
                if (columnbegin == 0)
                    columnbegin = cp.column;
                Codepoints += cp.cp;
                if (line == 0)
                    line = cp.line;
            }
            Token& operator=(const Codepoint& cp) {
                line = cp.line;
                columnbegin = cp.column;
                columnend = cp.column;
                Codepoints = cp.cp;
            }

            int line;
            int columnbegin;
            int columnend;
            TokenType type;
            std::wstring Codepoints;
        };
        struct FileStreamer {
            int current;
            std::vector<Codepoint> codepoints;
            int line;
            int column;
            std::wifstream file;
            FileStreamer(std::wstring filename)
            : file(filename, std::ios::in | std::ios::binary) {
                line = 0;
                column = 0;
                current = 0;
                // Extract all the codepoints immediately.
                Codepoint cp;
                while(*this >> cp)
                    codepoints.push_back(cp);
            }
            operator bool() {
                return current != codepoints.size();
            }
            FileStreamer& operator>>(Codepoint& cp) {
                if (*this) {
                    cp = codepoints[current];
                    current++;
                }
                return *this;
            }
            void putback() {
                if (current > 0)
                    current--;
            }
        };
        std::vector<Token> tokens;
        FileStreamer stream;
    public:
        Lexer(std::wstring file)
            : stream(file) {}
        void operator()();
    };
}

实施:

代码语言:javascript
复制
void Wide::Lexer::operator()() {
    Codepoint cp;
    Token current;
    auto end_current_token = [&] {
        if (current != Token()) {
            tokens.push_back(current);
            current = Token();
            cp = Codepoint();
        }
    };
    auto check = [&](wchar_t codepoint) -> bool {
        if (cp == codepoint) {
            end_current_token();
            tokens.push_back(cp);
            return true;
        }
        return true;
    };
    auto is_whitespace = [&](wchar_t codepoint) {
        return codepoint == L' ' || codepoint == L'\n' || codepoint == L'\t';
    };
    auto is_newline = [&](wchar_t codepoint) {
        return codepoint == L'\n';
    };
    while(stream >> cp) {
        // check for whitespace or comment first
        if (is_whitespace(cp.cp)) {
            end_current_token();
            continue;
        }

        if (cp == L'/') {
            end_current_token();
            Codepoint backup = cp;
            stream >> cp; // no need to check the stream for failure
            if (cp == L'/') {
                while(stream >> cp && !is_newline(cp.cp));
                continue;
            }
            // didn't find comment.
            tokens.push_back(backup);
            // put the other codepoint back
            stream.putback();
            continue;
        }
        if (check(L'.')) continue;
        if (check(L',')) continue;
        if (check(L'-')) continue;
        if (check(L';')) continue;
        if (check(L'*')) continue;
        if (check(L'&')) continue;
        if (check(L'^')) continue;
        if (check(L'%')) continue;
        if (check(L'"')) continue;
        if (check(L'!')) continue;
        if (check(L':')) continue;
        if (check(L'~')) continue;
        if (check(L'/')) continue;
        if (check(L'>')) continue;
        if (check(L'<')) continue;
        if (check(L'|')) continue;
        if (check(L')')) continue;
        if (check(L'(')) continue;
        if (check(L'[')) continue;
        if (check(L']')) continue;
        if (check(L'}')) continue;
        if (check(L'{')) continue;
        // Identifier/keyword

        current += cp;
    }
}
int main() {
    Wide::Lexer Input(L"Input.txt");
}

除了像一对情侣一样的管道工程,就是这样。这就是整个程序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-23 00:50:45

我不知道为什么编译器会抱怨operator->,但我认为这要么是编译器的错误,要么是Token是在其他地方定义的。也许赋值以某种方式被重新安排为通过函数指针的调用。

在任何情况下,我都能够通过使用显式名称空间范围解析限定符来编译代码。试试这个:

代码语言:javascript
复制
auto end_current_token = [&] {
        using namespace Wide;
    if (current != Wide::Lexer::Token()) {
        tokens.push_back(current);
        current = Wide::Lexer::Token();
        cp = Wide::Lexer::Codepoint();
    }
};

我相信--但我不敢肯定--在lambda的上下文中无论如何都需要这个显式的解决方案。

我会做一些更多的研究,看看你为什么会有这个问题。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7860998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档