不明确的语法
#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
}
为什么主函数中右大括号之前的最后两行给出错误?为什么表达式 MYINT() 在不同上下文中的处理方式不同?任何标准参考都会有所帮助。
#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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据上下文,
MYINT()
可以解释为MYINT
类型的表达式,或不带参数并返回MYINT
的函数类型说明符代码>.在某些情况下,表达式或类型说明符有效,这会产生歧义。如果可能的话,可以通过将其解释为类型说明符来解决此问题(编辑:C++03 8.2/2,如果您想要标准参考)。
sizeof
可以采用表达式或带括号的类型说明符作为参数,从而产生歧义。所以这里MYINT()
被解释为类型说明符;然后你会得到一个错误,因为sizeof
不能应用于函数类型。编辑:您可以通过删除括号来修复错误,以便将其解释为表达式 (
sizeof MYINT()
),添加额外的括号,使其不是有效的类型说明符 (sizeof((MYINT()))
),或将其更改为正确的类型 (sizeof(MYINT)
)。cout << MYINT()
是明确的,所以不应该有错误,而且我的编译器上确实没有错误。错误是什么,你的编译器是什么?MYINT()
can, depending on context, be interpreted as an expression of typeMYINT
, or a specifier of a function type taking no arguments and returningMYINT
.In some situations, where either an expression or a type specifier is valid, this gives an ambiguity; this is resolved by interpreting it as a type specifier if possible (EDIT: C++03 8.2/2, if you want a Standard reference).
sizeof
can take either an expression, or a parenthesised type specifier, as it's argument, giving this ambiguity. So hereMYINT()
is interpreted as a type specifier; then you get an error, sincesizeof
can't be applied to a function type.EDIT: You can fix the error by removing the parentheses so it will be interpreted as an expression (
sizeof MYINT()
), adding extra parentheses so it isn't a valid type specifier (sizeof((MYINT()))
), or changing it to the correct type (sizeof(MYINT)
).cout << MYINT()
is unambiguous, so there should be no error, and indeed there isn't on my compiler. What is the error, and what is your compiler?如果您的
MINTINT
是typedef int MYINT
那么MYINT()
不是一个函数,而是int()
这是默认初始化,相当于int y = 0
或int y = int(0)
。你的第二行,即
cout <<出于同样的原因,MYINT()
使用g++ -Wall -ansi -pedantic
可以正确编译。但是
g++
会抱怨sizeof
并出现以下错误error: invalid application of "sizeof" to a function type
因为它解释MYINT()
作为“对 int 的默认构造函数的调用”(编辑:这是不正确的)“返回 MYINT 的函数类型是不允许的”(编辑< /strong>:这是正确的答案,请参阅迈克的)。但这与typedef
无关。摘要:
编辑(再次)
正如评论中所说,
cout
行对您不起作用,这是因为您可能忘记包含;
。编辑
另请参阅 Mike Seymour 的回答,了解
sizeof
的歧义。If your
MINTINT
istypedef int MYINT
thenMYINT()
is not a function but isint()
which is a default initialization, equivallent toint y = 0
orint y = int(0)
.Your second line, ie
cout << MYINT()
compiles correctly for me withg++ -Wall -ansi -pedantic
for the same reason.But
g++
will complain for thesizeof
with the following errorerror: invalid application of "sizeof" to a function type
because it interpretsMYINT()
as "a call to a default constructor for int" (EDIT: this is not correct) "a function type returning MYINT which is not allowed" (EDIT: this is the correct answer, see Mike's). But this a nothing to do with thetypedef
.Summary:
Edit (again)
As said in the comment is the
cout
lines doesn't work for you, this is because you probably forgot toinclude <iostream>
.Edit
Look also the answer of Mike Seymour for an explanation of the ambiguity with
sizeof
.我没有看到
cout << 出现任何错误MYINT();
行。但是,我发现cout << 的
行。问题在于'sizeof' 无效应用于函数类型
。 sizeof(MYINT());MYINT()
周围的()
。 C++ 标准对sizeof
及其解析方式进行了这样的规定:sizeof unary-expression
和sizeof ( type-id )
之间存在解析歧义>。通过使用更长的匹配来解决。它将sizeof (MYINT())
解析为sizeof ( type-id )
,MYINT()
是一个函数类型,因此您会看到错误。I do not see any error for the
cout << MYINT();
line. However I seeinvalid application of 'sizeof' to a function type
for thecout << sizeof(MYINT());
line. The problem is the()
aroundMYINT()
. The C++ standard says this aboutsizeof
and how it is parsed:There is a parsing ambiguity between
sizeof unary-expression
andsizeof ( 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.cout << MYINT();
不起作用,因为cout
未定义。一旦你执行了#include
和using std::cout
,它就会正常工作。sizeof(MYINT())
确实不起作用,但sizeof(int())
也不起作用,所以这是可以预料的。sizeof(MYINT)
可以正常工作。它不是。在每种情况下,
MYINT()
的行为都与int()
完全相同。cout << MYINT();
doesn't work becausecout
is not defined. Once you do#include <iostream>
andusing std::cout
, it will work fine.sizeof(MYINT())
does indeed not work, butsizeof(int())
doesn't work either, so that's to be expected.sizeof(MYINT)
will work just fine.It's not. In every case
MYINT()
behaves exactly likeint()
.