Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistencies with "object declarations", "declarations that declare objects" and "class members of object type" #1280

Open
RealLitb opened this issue Dec 21, 2016 · 6 comments
Labels
cwg Issue must be reviewed by CWG.

Comments

@RealLitb
Copy link

RealLitb commented Dec 21, 2016

Richard asked me to add the following std-discussion content as an issue (the original question was about "A variable is introduced by the declaration of a reference other than a non-static data member or of an object.").

I still don't understand. I can see why a non-static data member is
not the declaration of an object.. because the (sub-) object is only
there once we create an object of the membering class. But how do
references differ in that regard? They too are not "there
immediately", but require an object of the membering class to
necessiate the beginning of the lifetime of a reference for each such
object.

I suppose the problem is in the ambiguity of what a "declaration of a
reference" is. I believe the intent here is that a "declaration of a
reference" is a declaration that declares a particular name to be of
reference type (which a non-static data member declaration of reference type
does), not necessarily a declaration that causes the lifetime of a
particular reference to begin. Conversely, a "declaration of an object" is
intended to be interpreted as a declaration that declares a particular name
to name a specific object (which a non-static data member declaration does
not).

That is:

struct A {
  int &a;
  int b;
};

'a' is a declaration of a reference, and so we need another condition to
restrict it from being a variable. 'b' is /not/ a declaration of an object,
so we don't need to say anything further.

Suggestions on how to reword this to make it clearer would be welcome :)

OK, thanks I see. But, is this intent worded somewhere in the spec
aswell? As far as declarations that contain decl-specifier-seq are
concerned we have at 7p10 that says

"If the decl-specifier-seq contains no typedef specifier, the declaration is called a function declaration if the type associated with the name is a function type ([dcl.fct]) and an object declaration otherwise.".

Am am not totally sure whether this only applies to
simple-declarations (because of the location of that paragraph), or
whether it applies to all declarations that contain
decl-specifier-seqs. Another thing is, because it indicate that an
"object declaration" is not quite the same as a "declaration of an
object", it becomes very involved. Are the following summaries correct
based on your answers and the spec?

struct A  {
   int &b; // Not the declaration of an object. But an object declaration. And the declaration of a reference
   int c; // Not the declaration of an object. But an object declaration
};

extern int &d; // Not the declaration of an object. But an object declaration. And the declaration of a reference
int e; // The declaration of an object. And an object declaration. But not the declaration of a reference

Are they correct concerning the intent? Would it not make sense to
distinguish between object declarations and reference declarations?
Other places in the spec seem to use "object declaration" as implying
that they declare objects, like 7.1.5p9:

"A constexpr specifier used in an object declaration declares the object as const."

On the other hand, there's evidence that other places use "object
declaration" to mean "declaration that gives names an object or
reference type", such as 8.2p1:

"In that context, the choice is between a function declaration with a redundant set of parentheses around a parameter name and an object declaration with a function-style cast as the initializer."

This indicates that this term "object declaration" was not updated
properly when references were added to the language?

@RealLitb
Copy link
Author

I would like to mention that it even calls "extern void(&r)();" an "object declaration". This is where the analogy with "object pointer" breaks down and IMO the very least that should be done with this term is to ensure that references to functions are not included.

@jensmaurer
Copy link
Member

jensmaurer commented Dec 27, 2016

It seems to me that [dcl.dcl]/10 is rather explicit that the world of (simple-)declarations has three partitions: typedef declarations, function declarations, and object declarations. The latter term therefore includes "reference to function" declarations. This distinction seems to be mostly used in [dcl.ambig.res] (correctly, it seems) plus a single use in [dcl.constexpr]. (constexpr on a reference declaration does make sense; it guarantees compile-time evaluation of the lvalue to which the reference binds.)

"declaration of an object" seems to be used only in notes and in [basic]/6.

My suggestion would be to add a note in [dcl.dcl]/10 highlighting the fact that "object declaration" also applies to references, even though they are not objects. And maybe rephrase the use in [dcl.constexpr] so that we're using more explicit terms there.

@jensmaurer jensmaurer added the decision-required A decision of the editorial group (or the Project Editor) is required. label Dec 28, 2016
@RealLitb
Copy link
Author

RealLitb commented Dec 28, 2016 via email

@jensmaurer jensmaurer added cwg Issue must be reviewed by CWG. and removed decision-required A decision of the editorial group (or the Project Editor) is required. labels Mar 2, 2017
@jensmaurer jensmaurer added decision-required A decision of the editorial group (or the Project Editor) is required. and removed cwg Issue must be reviewed by CWG. labels Jul 25, 2019
@sdkrystian
Copy link
Contributor

Perhaps it would be worthwhile to specify what "a declaration of an object" is explicitly.

@sdkrystian
Copy link
Contributor

Addtionally, it should be specified that a declaration of a static data member is the declaration of an object (as of now, it just declares a member)

@jensmaurer jensmaurer removed the decision-required A decision of the editorial group (or the Project Editor) is required. label Sep 10, 2019
@jensmaurer jensmaurer added the decision-required A decision of the editorial group (or the Project Editor) is required. label Sep 24, 2019
@jensmaurer jensmaurer added cwg Issue must be reviewed by CWG. and removed decision-required A decision of the editorial group (or the Project Editor) is required. labels Nov 4, 2019
@jensmaurer
Copy link
Member

Editorial meeting: Address as a CWG issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cwg Issue must be reviewed by CWG.
Projects
None yet
Development

No branches or pull requests

3 participants