gpt4 book ai didi

c++ - operator>>(,) 重载以一种令人惊讶的方式表现

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:11 26 4
gpt4 key购买 nike

嗯,也许这并不令人惊讶。 Stackoverflow 对此有很多问题和贡献。只是他们不完全切中要害。

这里是 C++ 标准的摘录(实际上是 C++14 草案,但我假设这段话在当前的 C++11 标准中是相同的):

An operator function shall either be a non-static member function or be a non-member function that has at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration. It is not possible to change the precedence, grouping, or number of operands of operators. The meaning of the operators =, (unary) &, and , (comma), predefined for each type, can be changed for specific class and enumeration types by defining operator functions that implement these operators. Operator functions are inherited in the same manner as other base class functions.

因此,根据我的理解,将一种类类型和一种非类类型作为运算符>>(,) 的参数是完全合法的。该标准没有说“第一”或“第二”参数。其中只有“一个”必须是类类型。

这是一个令我惊讶的代码片段:

int operator>> ( int v, std::function<int(int)> transformer )
{
int v1 = transformer(v);
DGS::CLogger::GetLogger()->Log<int>(&IntFormatter, v1 );
return v1;
}
static int DoItLoggedAndCompact( int value )
{
int x = operator>>( operator>>(value, DoIt) , AnotherIntCalculation ); // compiles and works!
return x;
// The following line produces (with clang++):
// error: invalid operands to binary expression ('int' and 'int (*)(int)')
// return value >> DoIt >> AnotherIntCalculation; :
}

请注意,“函数指针不是类类型”虽然是正确的陈述,但并不是一个全面的答案。正如您在以 int x = ... 和 operator>> 第二个参数的定义开头的重写代码中看到的那样,函数指针被静默地转换为 std::function。

我在标准的任何地方都找不到一段说明这两种(​​据称)同义形式的转换规则不同。

那么,这是编译器错误、社区范围内对 C++ 规范的过度解释还是……我们在这里看到的其他问题?或者只是我的一些愚蠢的疏忽?

最佳答案

但是“函数指针不是类类型”是正确答案。

13.6条中有如下注释:

operator overload resolution occurs only when an operand expression originally has class or enumeration type

规范规则在13.3.1.2(强调我的):

If no operand of an operator in an expression has a type that is a class or an enumeration, the operator is assumed to be a built-in operator and interpreted according to Clause 5.

If either operand has a type that is a class or an enumeration, a user-defined operator function might be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator. In this case, overload resolution is used to determine which operator function or built-in operator is to be invoked to implement the operator. Therefore, the operator notation is first transformed to the equivalent function-call notation as summarized in Table 11.

仅当其中一个操作数是类类型时,这些形式才是同义的。由于两者都不在此处,因此不会重写为 operator>>()

关于c++ - operator>>(,) 重载以一种令人惊讶的方式表现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27826824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com