我不明白为什么不能在运算符的 RHS 上使用初始化列表。考虑:
class foo { };
struct bar
{
template<typename... T>
bar(T const&...) { }
};
foo& operator<<(foo& f, bar const&) { return f; }
int main()
{
foo baz;
baz << {1, -2, "foo", 4, 5};
return 0;
}
最新的 Clang(以及 gcc)提示:
clang.cc:14:9: error: initializer list cannot be used on the right hand side of operator '<<'
baz << {1, -2, "foo", 4, 5};
^ ~~~~~~~~~~~~~~~~~~~~
^ ~~~~~~~~~~~~~~~
为什么 C++ 标准会禁止这样做?或者换句话说,为什么这会失败而不是
baz << bar{1, -2, "foo", 4, 5};
?
最佳答案
确实,C++11 的最终版本不允许在二元运算符的右侧(或左侧)使用初始化列表。
首先,initializer-lists 不是标准第 5 节中定义的表达式。函数和二元运算符的参数通常必须是表达式,第 5 节中定义的表达式语法不包括大括号初始化列表的语法(即纯初始化列表;请注意,类型名 < em>后跟一个大括号初始化列表,例如 bar {2,5,"hello",7}
是一个表达式)。
为了能够方便地使用纯初始化列表,标准定义了各种异常,总结如下(非规范)注释:
§8.5.4/1 [...] Note: List-initialization can be used
— as the initializer in a variable definition (8.5)
— as the initializer in a new expression (5.3.4)
— in a return statement (6.6.3)
— as a function argument (5.2.2)
— as a subscript (5.2.1)
— as an argument to a constructor invocation (8.5, 5.2.3)
— as an initializer for a non-static data member (9.2)
— in a mem-initializer (12.6.2)
— on the right-hand side of an assignment (5.17)
[...]
上面的第四项明确允许纯初始化列表作为函数参数(这就是 operator<<(baz, {1, -2, "foo", 4, 5});
起作用的原因),第五项允许在下标表达式中使用它(即作为 operator[]
的参数,例如 mymap[{2,5,"hello"}]
是合法的) , 最后一项允许它们位于 assignments 的右侧(但不是一般的二元运算符)。
二元运算符没有这样的异常(exception),例如 +
, *
或 <<
,因此您不能在它们的任一侧放置一个纯初始化列表(即前面没有类型名的列表)。
至于原因,draft/discussion paper N2215由 Stroustrup 和 Dos Reis 从 2007 年开始提供了很多关于初始化列表在各种上下文中的许多问题的见解。具体来说,有一节是关于二元运算符的(第 6.2 节):
Consider more general uses of initializer lists. For example:
v = v+{3,4}; v = {6,7}+v;
When we consider operators as syntactic sugar for functions, we naturally consider the above equivalent to
v = operator+(v,{3,4}); v = operator+({6,7},v);
It is therefore natural to extend the use of initializer lists to expressions. There are many uses where initializer lists combined with operators is a “natural” notation.
However, it is not trivial to write a LR(1) grammar that allows arbitrary use of initializer lists. A block also starts with a { so allowing an initializer list as the first (leftmost) entity of an expression would lead to chaos in the grammar.
It is trivial to allow initializer lists as the right-hand operand of binary operators, in subscripts, and similar isolated parts of the grammar. The real problem is to allow;a={1,2}+b;
as an assignment-statement without also allowing;{1,2}+b;
. We suspect that allowing initializer lists as right-hand, but nor [sic] as left-hand arguments to most operators is too much of a kludge, [...]
换句话说,初始化器列表在右侧没有启用因为它们在左侧没有启用,并且它们在左侧没有启用-因为这会给解析器带来太大的挑战。
我想知道是否可以通过为初始化列表语法选择不同的符号而不是花括号来简化问题。
关于c++ - 初始化器列表和运算符的 RHS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420448/