c++ - 运算符的初始化列表和 RHS

标签 c++ c++11 operators initializer-list

我不明白为什么不能在运算符的 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 的最终版本不允许在二元运算符的右侧(或左侧)使用初始化列表。

首先,初始化列表不是标准 §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"}] 是合法的) ,最后一项允许它们位于赋值的右侧(但不是一般的二元运算符)。

二元运算符没有这样的异常(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/42191661/

相关文章:

c++ - 运算符优先级(bool 之前是否为 void*?)

c++ - CM_Get_DevNode_Property_Keys 有没有办法获取 key 的名称

c++ - 无法弄清楚我的 Makefile 有什么问题?

c++ - 是否建议始终使用 'binary' 模式打开文件?

c++ - std::forward_list::remove_if 谓词的要求

Python:有什么区别 - abs 和 operator.abs

C++ 删除 vector 中的对象

c++ - decltype - "the only context in which a variable defined as a reference is not treated as a synonym for the object to which it refers"?

c++ - 如何将位域成员的所有位设置为 1

java - BigInteger 和 BigDecimal 的平方根和++ 运算符