Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler chosing prefix ++ when postfix is missing - who says?

Tags:

c++

visual-c++

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?

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?

like image 460
Arbalest Avatar asked Aug 17 '11 02:08

Arbalest


2 Answers

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++ 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.

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

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.

like image 174
Alok Save Avatar answered Nov 15 '22 15:11

Alok Save


It should be a Microsoft specific extension. Because, at least g++ is strict about prefix and postfix operators. Here is the demo link.

like image 1
iammilind Avatar answered Nov 15 '22 15:11

iammilind