操作员优先超载
我正在尝试为简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的表达式将被编译为类似以下内容,这可能会解释评估顺序:
或者
取决于您的
operator,()
的定义方式(以及COLUMN
和已定义TYPE
宏)。Your expression would be compiled to something like the following, which might explain the evaluation order:
or
depending on how your
operator,()
is defined (and maybe how theCOLUMN
andTYPE
macros are defined).必须首先计算
[]
内的表达式 - 它需要作为参数传入,这就是为什么您会看到首先调用operator,
的原因。The expression inside the
[]
has to be evaluated first - it needs to be passed in as the argument, which is why you see theoperator,
being called first.因为为了调用
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 usesoperator,
to get a single result with which to call youroperator[]
.由于
[]
运算符采用单个参数,因此它会等待[]
之间的整个表达式被求值,然后再对其自身进行求值。Because the
[]
operator takes a single argument, it waits for the entire expression between the[]
to be evaluated before it is evaluated itself.看起来您正在使用
operator,()
的结果作为operatot[]()
的参数,因此首先评估前者是合乎逻辑的,如下所示f(g(x))
。此外,运行时不会“解析”任何内容。 C++ 是一种编译语言。
It looks like you are using the result of
operator,()
as the argument ofoperatot[]()
, so it's only logical that the former is evaluated first, as inf(g(x))
.Also, nothing is "parsed" at runtime. C++ is a compiled language.