编译器选择前缀++当后缀丢失 - 谁说? [英] Compiler chosing prefix ++ when postfix is missing - who says?

查看:182
本文介绍了编译器选择前缀++当后缀丢失 - 谁说?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您为用户定义类型定义前缀运算符++并且不提供后缀版本时,编译器(至少在Visual C ++中)将在您的代码调用缺少的POSTFIX版本时使用PREFIX版本。

When you define a prefix operator++ for your user defined type and you don't provide a postfix version, the compiler (in Visual C++ at least) will use the PREFIX version when your code calls the missing POSTFIX version.

至少它会给你一个警告。但是,我的问题是:为什么不给你一个错误的未定义的成员函数?

At least it will give you a warning. But, my question is: Why doesn't it just give you an error for the undefined member function?

我看到这第一手,另一个帖子和其他地方,但我不能在实际的C ++标准中找到这一点。我的第二个和第三个问题是...它在标准的某个地方吗?

I have seen this first hand, and have seen it mentioned in another post and elsewhere, but I cannot find this in the actual C++ standard. My second and third questions are... Is it in the standard somewhere? Is this a Microsoft-specific handing of the situation?

推荐答案

实际上,在这种情况下,MSVC的行为要比GCC更加智能。

这是一个MSVC编译器扩展,C ++标准明确允许这样的行为。

Actually, In this case the MSVC behaves much more intelligently than GCC.
This is a MSVC compiler extension and the C++ standard explicitly allows for such an behavior.

C ++标准:

第1.4 / 8节:

可以具有扩展(包括附加的库函数),只要它们不改变任何形式良好的程序的行为。 需要执行程序来诊断使用此类扩展程序的程序是否符合本国际标准。

C++ Standard:
Section 1.4/8:
A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program. Implementations are required to diagnose programs that use such extensions that are ill-formed according to this International Standard. Having done so, however, they can compile and execute such programs.

在这种情况下,MSVC会适当地诊断postfix的问题不可用,并专门定义警告,

编译器警告(级别1)C4620

编译器警告(级别1)C4621

In this case, MSVC appropriately diagnoses the problem that the postfix is not available and it specifically defines warnings,
Compiler Warning (level 1) C4620
Compiler Warning (level 1) C4621

此外,它还提供了一个功能, > / Za 。总的来说,我想说这是MSVC实际上比GCC表现更好的情况之一。

Also, it provides you a facility to disable the MSVC specific extensions by using /Za. Overall, I would say this is one of the instances where MSVC actually behaves better than GCC.

这篇关于编译器选择前缀++当后缀丢失 - 谁说?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆