元操作符的优先级是什么?

7
meta-operator ...是用于解包模板类型参数包的运算符,它的优先级非常低,但具体有多低呢?C++标准规定:“运算符的优先级没有直接指定,但可以从语法中推导出来。”是否有人能接受挑战呢?当然,在C++03运算符优先级表中并没有...。好的,如果...不是运算符,那么是什么决定了std::forward<Args>(args)...适用于整个序列std::forward<Args>(args)而不仅仅是(args)呢?

这个问题是什么?它不是一个运算符,也不与运算符交互。也许你应该提供一个例子。 - Gene Bushuyev
1
@FredOverflow:关于一些 C++0x 的问题,你询问的我知道或者可以猜到,但是有些(包括这个)我不知道。但是请继续提问,因为我从你的问题中学习了C++0x!像“懒鸟追随他人”的道理一样。 :-) - Cheers and hth. - Alf
@Alf:只要每个人都轮流领跑,那就是最好的懒惰方式。 - Steve Jessop
@Steve:如果我们轮流回答问题,那么我们也必须循环提问,否则这就不是真正的轮流了。(毕竟这是一个问答网站。)然而,就像回答问题一样,我希望将提问任务留给那些做得最好的人。而Fred已经证明他能想出非常好的问题。只要我们都受益,为什么要打破这种平衡呢?:) - sbi
@sbi:你是在暗示我的答案很烂吗? ;) - fredoverflow
2个回答

5

它似乎不是运算符。参考N3092(抱歉,我手边没有更新的草案)。

[14.5.3] 4/ A pack expansion is a sequence of tokens that names one or more parameter packs, followed by an ellipsis. The sequence of tokens is called the pattern of the expansion; its syntax depends on the context in which the expansion occurs. Pack expansions can occur in the following contexts:

  • In an initializer-list (8.5); the pattern is an initializer-clause.
  • In a base-specifier-list (10); the pattern is a base-specifier.
  • In a mem-initializer-list (12.6.2); the pattern is a mem-initializer.
  • In a template-argument-list (14.3); the pattern is a template-argument.
  • In a dynamic-exception-specification (15.4); the pattern is a type-id.
  • In an attribute-list (7.6.1); the pattern is an attribute.
  • In a capture-list (5.1.2); the pattern is a capture. [Example:

    template<class ... Types> void f(Types ... rest);
    template<class ... Types> void g(Types ... rest) {
        f(&rest ...); // “&rest ...” is a pack expansion; “&rest” is its pattern
    }
    

    — end example]


1
这是一个非常出色的答案,但不幸的是并没有太多启发性。 :-) - Omnifarious
1
让它更加清晰易懂的是每种情况的示例以及具体案例,展示了...的位置如何影响编译器认为你的意思是哪种情况。 - Omnifarious
@Omnifarious: 标准文本将...的不同用法分散在文本中。相关章节包含示例。 - Alexandre C.

1
根据方便的超链接C++ BNF语法,函数调用看起来像这样:

后缀表达式(表达式列表可选

表达式列表只是一个初始化器列表,它看起来像这样:

初始化器子句 ...可选
初始化器列表,初始化器子句 ...可选

其中省略号是包扩展符号。

初始化器子句可以是赋值表达式大括号初始化列表

所有这些都是为了说明省略号比任何实际运算符的语法优先级都低,因此例如以下两种写法是等价的:

foo(args ^= 0x1234...) and foo((args ^= 0x1234)...)

foo(x ? args : 42...) and foo((x ? args : 42)...) 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接