不明确的语法

发布于 2024-09-17 22:00:32 字数 382 浏览 3 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青衫负雪 2024-09-24 22:00:32

根据上下文,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 type MYINT, or a specifier of a function type taking no arguments and returning MYINT.
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 here MYINT() is interpreted as a type specifier; then you get an error, since sizeof 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?

爱已欠费 2024-09-24 22:00:32

如果您的 MINTINTtypedef int MYINT 那么 MYINT() 不是一个函数,而是 int() 这是默认初始化,相当于 int y = 0int 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无关。

摘要:

#include <iostream>
typedef int myint;
int main()
{
int y = myint();
int z = myint(0);
std::cout << y << z; // Will output 0 0
std::cout << std::endl << myint(0) << myint(); // Will output 0 0
std::cout << sizeof(int()); // The error is here; same with sizeof(myint())
}

编辑(再次)

正如评论中所说,cout 行对您不起作用,这是因为您可能忘记包含;

编辑
另请参阅 Mike Seymour 的回答,了解 sizeof 的歧义。

If your MINTINT is typedef int MYINT then MYINT() is not a function but is int() which is a default initialization, equivallent to int y = 0 or int y = int(0).

Your second line, ie cout << MYINT() compiles correctly for me with g++ -Wall -ansi -pedantic for the same reason.

But g++ will complain for the sizeof with the following error error: invalid application of "sizeof" to a function type because it interprets MYINT() 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 the typedef.

Summary:

#include <iostream>
typedef int myint;
int main()
{
int y = myint();
int z = myint(0);
std::cout << y << z; // Will output 0 0
std::cout << std::endl << myint(0) << myint(); // Will output 0 0
std::cout << sizeof(int()); // The error is here; same with sizeof(myint())
}

Edit (again)

As said in the comment is the cout lines doesn't work for you, this is because you probably forgot to include <iostream>.

Edit
Look also the answer of Mike Seymour for an explanation of the ambiguity with sizeof.

乖不如嘢 2024-09-24 22:00:32
// OK. Implicit conversion to int.
int y = MYINT();          

// OK. Implicit conversion again. Which compiler do you use?
cout << MYINT();          

// Invalid. Tries to get size of a function that returns MYINT,
// because sizeof expects a type-id and according to 8.2/2,
// which is forbidden according to the C++ Standard 5.3.3/1
cout << sizeof(MYINT());  
// Do you want this instead?
cout << sizeof(MYINT);  
// OK. Implicit conversion to int.
int y = MYINT();          

// OK. Implicit conversion again. Which compiler do you use?
cout << MYINT();          

// Invalid. Tries to get size of a function that returns MYINT,
// because sizeof expects a type-id and according to 8.2/2,
// which is forbidden according to the C++ Standard 5.3.3/1
cout << sizeof(MYINT());  
// Do you want this instead?
cout << sizeof(MYINT);  
千秋岁 2024-09-24 22:00:32

我没有看到 cout << 出现任何错误MYINT(); 行。但是,我发现 cout << 的 'sizeof' 无效应用于函数类型。 sizeof(MYINT()); 行。问题在于 MYINT() 周围的 ()。 C++ 标准对 sizeof 及其解析方式进行了这样的规定:

sizeof unary-expression
sizeof ( type-id )

sizeof unary-expressionsizeof ( type-id ) 之间存在解析歧义>。通过使用更长的匹配来解决。它将 sizeof (MYINT()) 解析为 sizeof ( type-id )MYINT() 是一个函数类型,因此您会看到错误。

I do not see any error for the cout << MYINT(); line. However I see invalid application of 'sizeof' to a function type for the cout << sizeof(MYINT()); line. The problem is the () around MYINT(). The C++ standard says this about sizeof and how it is parsed:

sizeof unary-expression
sizeof ( type-id )

There is a parsing ambiguity between sizeof unary-expression and sizeof ( type-id ). It is resolved by using longer match. It parses sizeof (MYINT()) as sizeof ( type-id ), MYINT() is a function type and thus you see the error.

小草泠泠 2024-09-24 22:00:32

为什么主函数中右大括号之前的最后两行会出错?

cout << MYINT(); 不起作用,因为 cout 未定义。一旦你执行了#includeusing std::cout,它就会正常工作。

sizeof(MYINT()) 确实不起作用,但 sizeof(int()) 也不起作用,所以这是可以预料的。 sizeof(MYINT) 可以正常工作。

为什么表达式 MYINT() 在不同的上下文中会有不同的处理方式?

它不是。在每种情况下,MYINT() 的行为都与 int() 完全相同。

Why the last two lines in the main function before the closing brace give error?

cout << MYINT(); doesn't work because cout is not defined. Once you do #include <iostream> and using std::cout, it will work fine.

sizeof(MYINT()) does indeed not work, but sizeof(int()) doesn't work either, so that's to be expected. sizeof(MYINT) will work just fine.

Why is the expression MYINT() treated differently in different contexts?

It's not. In every case MYINT() behaves exactly like int().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文