#include <iostream>
using namespace std;
typedef int MYINT;
int main()
{
int y = MYINT(); // As expected, y = 0; value initialization
cout << MYINT(); // Error
cout << sizeof(MYINT()); // Error
}
Why the last two lines in the main function before the closing brace give error? Why is the expression MYINT() treated differently in different contexts? Any Standard reference will be helpful.
I do not see any error for the
cout << MYINT();line. However I seeinvalid application of 'sizeof' to a function typefor thecout << sizeof(MYINT());line. The problem is the()aroundMYINT(). The C++ standard says this aboutsizeofand how it is parsed:There is a parsing ambiguity between
sizeof unary-expressionandsizeof ( type-id ). It is resolved by using longer match. It parsessizeof (MYINT())assizeof ( type-id ),MYINT()is a function type and thus you see the error.