C++ 没有参数的构造函数上没有括号是语言标准吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2318650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:00:40  来源:igfitidea点击:

Is no parentheses on a constructor with no arguments a language standard?

c++constructorstandards

提问by mring

I was compiling a C++ program in Cygwin using g++ and I had a class whose constructor had no arguments. I had the lines:

我正在使用 g++ 在 Cygwin 中编译一个 C++ 程序,我有一个类的构造函数没有参数。我有以下几行:

MyClass myObj();
myObj.function1();

And when trying to compile it, I got the message:

在尝试编译它时,我收到了消息:

error: request for member 'function1' in 'myObj', which is of non-class type 'MyClass ()()'

After a little research, I found that the fix was to change that first line to

经过一番研究,我发现修复方法是将第一行更改为

MyClass myObj;

I could swear I've done empty constructor declarations with parentheses in C++ before. Is this probably a limitation of the compiler I'm using or does the language standard really say don't use parentheses for a constructor without arguments?

我可以发誓我以前在 C++ 中做过带括号的空构造函数声明。这可能是我使用的编译器的限制,还是语言标准真的说不要在没有参数的构造函数中使用括号?

回答by CB Bailey

Although MyClass myObj();could be parsed as an object definition with an empty initializer or a function declaration the language standard specifies that the ambiguity is always resolved in favour of the function declaration. An empty parentheses initializer is allowed in other contexts e.g. in a newexpression or constructing a value-initializedtemporary.

尽管MyClass myObj();可以将其解析为具有空初始值设定项或函数声明的对象定义,但语言标准指定始终有利于函数声明来解决歧义。在其他上下文中允许使用空括号初始值设定项,例如在new表达式中或构造值初始化的临时

回答by Peter Alexander

This is called the most vexing parse issue. When the parser sees

这被称为最棘手的解析问题。当解析器看到

MyClass myObj();

It thinks you are declaring a function called myObjthat has no parameters and returns a MyClass.

它认为您正在声明一个myObj没有参数的调用函数并返回一个MyClass.

To get around it, use:

要绕过它,请使用:

MyClass myObj;

回答by suszterpatt

I found this in the C++ standard (§8.5.8):

我在 C++ 标准(第 8.5.8 节)中发现了这一点:

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

[Note: since () is not permitted by the syntax for initializer,

X a ();

is not the declaration of an object of class X, but the declaration of a function taking no argument and returning an X. The form () is permitted in certain other initialization contexts (5.3.4, 5.2.3, 12.6.2). —end note ]

初始值设定项为空括号集的对象,即 (),应进行值初始化。

[注意:由于 () 不允许用于初始化程序的语法,

X a ();

不是类 X 的对象的声明,而是不带参数并返回 X 的函数的声明。形式 () 在某些其他初始化上下文中是允许的(5.3.4、5.2.3、12.6.2)。——尾注]

回答by Liz Albin

This is a fairly well known issue, and isn't compiler dependent. Essentially, what you were doing was declaring a function returning type MyObj. Not surprisingly, you couldn't call its constructor. See the C++ faq litefor a good explanation

这是一个众所周知的问题,与编译器无关。本质上,您所做的是声明一个返回类型 MyObj 的函数。毫不奇怪,你不能调用它的构造函数。请参阅C++ faq lite以获得很好的解释

回答by sbk

MyClass myObj();

That's parsed as a function declaration, the function is called myObj, takes no arguments and returns MyClass object. I've never seen a compiler accepting that. On the other hand MyClass* myPtr = new MyClass();is acceptable, may be that got you confused?

这被解析为函数声明,该函数称为 myObj,不接受任何参数并返回 MyClass 对象。我从未见过编译器接受这一点。另一方面MyClass* myPtr = new MyClass();是可以接受的,可能会让你感到困惑吗?

回答by AProgrammer

Yet another most-vexing-parse hit. See for instance Sort function does not work with function object created on stack?

另一个最令人烦恼的解析命中。请参阅例如Sort 函数不适用于在堆栈上创建的函数对象?

回答by Ari

Your line makes the compiler think you are declaring a function named myObjwhich takes no arguments and returns a MyClass. This ambiguity resolution is indeed annoying.

您的行使编译器认为您正在声明一个名为的函数myObj,该函数不接受任何参数并返回一个MyClass. 这种歧义解析确实很烦人。

回答by JonH

The standard does not require parentheses.

该标准不需要括号。

int* x = new int;

is legal syntax.

是合法的语法。

In your case myclass myobj();is a function prototype. Whereas myclass myobj;is a variable.

在你的情况下myclass myobj();是一个函数原型。而myclass myobj;是一个变量。