操作员优先超载

发布于 2024-10-12 22:11:23 字数 442 浏览 3 评论 0原文

我正在尝试为简单的 SQL 命令创建一个 C++ 编译模型。例如,这可能是我必须能够处理的主函数的一部分:

CREATE_TABLE(books) [             // create("books");ovr[
    COLUMN(c1) TYPE(string),      // "title string",
    COLUMN(c2) TYPE(string),      // "author string",
    COLUMN(num1) TYPE(int)        // "price int"
                    ];

因此,为了做到这一点,我必须重载“[]”和“,”运算符。这样做之后,我发现“,”重载器是在“[]”重载器之前执行的。而我的猜测是“[]”应该首先执行。发生这种情况有什么特殊原因吗?或者仅仅是因为找到“]”时执行“[]”?

I am trying to make a C++ compiled model for simple SQL commands. For example this could be a part of my main function which i must be able to handle :

CREATE_TABLE(books) [             // create("books");ovr[
    COLUMN(c1) TYPE(string),      // "title string",
    COLUMN(c2) TYPE(string),      // "author string",
    COLUMN(num1) TYPE(int)        // "price int"
                    ];

So in order to do that i had to overload the "[]" and "," operators. After doing so, I figured out that the "," overloader is executed before the "[]" one. Whereas my guess would be that "[]" should be executed first. Is there any particular reason why this happens? Or is it simply because the "[]" is executed when "]" is found?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

土豪 2024-10-19 22:11:23

您的表达式将被编译为类似以下内容,这可能会解释评估顺序:

CREATE_TABLE(books).operator[](
    COLUMN(c1) TYPE(string).operator,(
        COLUMN(c2) TYPE(string).operator,(
            COLUMN(num1) TYPE(int) 
        )
    )
);

或者

CREATE_TABLE(books).operator[](
    operator,( COLUMN(c1) TYPE(string), 
        operator,( COLUMN(c2) TYPE(string), COLUMN(num1) TYPE(int))
    )
);

取决于您的operator,()的定义方式(以及COLUMN和已定义 TYPE 宏)。

Your expression would be compiled to something like the following, which might explain the evaluation order:

CREATE_TABLE(books).operator[](
    COLUMN(c1) TYPE(string).operator,(
        COLUMN(c2) TYPE(string).operator,(
            COLUMN(num1) TYPE(int) 
        )
    )
);

or

CREATE_TABLE(books).operator[](
    operator,( COLUMN(c1) TYPE(string), 
        operator,( COLUMN(c2) TYPE(string), COLUMN(num1) TYPE(int))
    )
);

depending on how your operator,() is defined (and maybe how the COLUMN and TYPE macros are defined).

笔落惊风雨 2024-10-19 22:11:23

必须首先计算 [] 内的表达式 - 它需要作为参数传入,这就是为什么您会看到首先调用 operator, 的原因。

The expression inside the [] has to be evaluated first - it needs to be passed in as the argument, which is why you see the operator, being called first.

一指流沙 2024-10-19 22:11:23

因为为了调用operator[],它需要一个参数。它将括号内的内容视为带逗号的表达式,并使用 operator, 获取单个结果来调用 operator[]

Because in order to call operator[], it needs a single parameter. It treats what's inside the brackets as an expression with commas, and uses operator, to get a single result with which to call your operator[].

ι不睡觉的鱼゛ 2024-10-19 22:11:23

由于 [] 运算符采用单个参数,因此它会等待 [] 之间的整个表达式被求值,然后再对其自身进行求值。

Because the [] operator takes a single argument, it waits for the entire expression between the [] to be evaluated before it is evaluated itself.

耀眼的星火 2024-10-19 22:11:23

看起来您正在使用 operator,() 的结果作为 operatot[]() 的参数,因此首先评估前者是合乎逻辑的,如下所示f(g(x))

此外,运行时不会“解析”任何内容。 C++ 是一种编译语言。

It looks like you are using the result of operator,() as the argument of operatot[](), so it's only logical that the former is evaluated first, as in f(g(x)).

Also, nothing is "parsed" at runtime. C++ is a compiled language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文