Topic: Using declaration and multiple declarations


Author: Hideaki Onaru <gomarine@mx1.ttcn.ne.jp>
Date: 2000/03/27
Raw View
Mr. Adler, thank you for your reply.

> > > These two declarations refer to the same object because they refer to
> > > the same name in the same namespace.
>
> > Is this stated in the Standard?
>
> Yes.
>
> In 3.3/1 it says,
>
> "Every name is introduced in some portion of program text called a
> declarative region, which is the largest part of the program in which
> that name is valid, that is, in which that name may be used as an
> unqualified name to refer to the same entity."
>
> When a name is introduced in namespace scope, then the name can be used
> as an unqualified name to refer to the same entity. An object is one
> kind of entity.

Well, in fact, I don't place much confidence in the description in the
Standard on concepts "declarative region" and "scope" because:

1) The definition of "declarative region" depends on that of "unqulified name
lookup", as quoted.
(Otherwise, how can we know/decide whether the name refer to the same entity
or not? Moreover, it is NOT the entity(s) that the lookup finds, but just declaration(s).)
2) The definition of "scope" (or "potential scope") depends on that of
"declarative region".
3) And, the definition of "unqualified name lookup" depends on that of "scope".

I've missed something?
I think the "declarative region" should be defined in syntactic wording, not
in semantic wording.
And, some statements which describe the case when two or more declarations
refer to the same entity are still needed.

----------

> > Then you mean that b) and c) refer to the same entity, but that violates
> > ODR?  This is the candidate answer 2) I wrote in the first posting.
> >
> > 2) Ill-formed, because the definition of i in B (line c)) is
> > redefinition of i in A (line a)), which violates ODR. That is, i of
> > c) refers to A::i because of b).
>
> I think that is the correct interpretation.
(The original program is as follows:

// program 2
namespace A {
    int i;        // a)
}
namespace B {
    using A::i;   // b)
    int i;        // c)
}

)

Then the example below is legal?

// program 2e
namespace A {
    extern int i; // not defined
}
namespace B {
    using A::i;   // not defined
    int i;        // doesn't violate ODR
}

In Draft, 7.3.3/1 was as following:

"A using-declaration introduces a name into the declarative region in which
the using-declaration appears. That name is a synonym for the name of some
entity declared elsewhere. A name specified in a using-declaration in a class
or namespace scope shall not already be a member in that scope. (...)"

And now, in the official Standard, the sentence

"A name specified in a using-declaration in a class or namespace scope shall
not already be a member in that scope."

has disappeared.
In the first posting, I wrote the following code was clearly illegal according
to the already disappeared statement:

// program 1
namespace A {
    int i;
}
namespace B {
    int i;
    using A::i;   // 'i' is already a member of B
}

Now, if the interpretation 2) is correct, this code is still illegal, but
because of another reason: violation of ODR.
If so, the following is to be NOT illegal (as program 2e):

// program 1e --- clearly ill-formed according to Draft
namespace A {
    extern int i; // not defined
}
namespace B {
    int i;
    using A::i;   // just a declaration, so doesn't violate ODR
}

Do you think this interpretation is right?

I'm not sure which interpretation is "better", the interpretation in which all
of the four programs (1, 1e, 2, 2e) are equally ill-formed, or the
interpretation in which only half of them (i.e., 1 and 2) are ill-formed.
But I guess that a part of the intention of deleting the sentence in 7.3.3/1
and rewriting 3.3/4 and 7.3.3/10 might be to get rid of asymmetry in treating
programs 1 and 2 (or, 1e and 2e), but is not to allow the program 1e to be legal.

----------

I would like to listen to your opinion.
Thank you.


Hideaki Onaru

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Darin Adler <darin@bentspoon.com>
Date: 2000/03/28
Raw View
In article <38DD6058.F1B0E107@mx1.ttcn.ne.jp>, gomarine@mx1.ttcn.ne.jp
wrote:

> Well, in fact, I don't place much confidence in the description in the
> Standard on concepts "declarative region" and "scope" because:
>
> 1) The definition of "declarative region" depends on that of
> "unqualified name lookup", as quoted. (Otherwise, how can we
> know/decide whether the name refer to the same entity or not?
> Moreover, it is NOT the entity(s) that the lookup finds, but just
> declaration(s).)
> 2) The definition of "scope" (or "potential scope") depends on that of
> "declarative region".
> 3) And, the definition of "unqualified name lookup" depends on that of
> "scope".
>
> I've missed something?

The definition of "declarative region" mentions "unqualified name
lookup", but I wouldn't say it "depends" on it. Unqualified name lookup
is defined in terms of declarative regions, not the other way around. I
have no trouble with the description of declarative regions, scope, and
unqualified name lookup in the standard.

It's useful to discuss specific issues, not a broad sense you have that
the wording is too vague.

On another topic, I'm not interested in the wording of the drafts and
the changes that led to the final standard except perhaps as background
material if we find a defect in the final standard.

> (The original program is as follows:
>
> // program 2
> namespace A {
>     int i;        // a)
> }
> namespace B {
>     using A::i;   // b)
>     int i;        // c)
> }
>
> )
>
> Then the example below is legal?
>
> // program 2e
> namespace A {
>     extern int i; // not defined
> }
> namespace B {
>     using A::i;   // not defined
>     int i;        // doesn't violate ODR
> }

I think that is legal.

> If so, the following is to be NOT illegal (as program 2e):
>
> // program 1e --- clearly ill-formed according to Draft
> namespace A {
>     extern int i; // not defined
> }
> namespace B {
>     int i;
>     using A::i;   // just a declaration, so doesn't violate ODR
> }

I see nothing in the standard that would make this illegal, although my
favorite compiler doesn't accept it.

I think that it is legal.

    -- Darin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hideaki Onaru <gomarine@mx1.ttcn.ne.jp>
Date: 2000/03/25
Raw View
Mr. Darin Adler, Thank you for your reply.
Your messages didn't appear on my news server.
(I found and read yours on www.remarq.com page.)
I didn't intend to ignore your messages.

>> namespace A {
>>     int i; // a)
>> }
>> namespace B {
>>     using A::i; // b)
>>     int i; // c)
>> }
>> is this also ill-formed?
>
> Yes.
>
> You really must get the final standard and *stop* using an out-of-date draft.
I've finally got it, and found some statements I quoted already rewritten.

> In this case, the section has been completely rewritten since the draft.
> The final standard says this in 7.3.3/10:
>
> "Since a using-declaration is a declaration, the restrictions on declarations
> of the same name in the same declarative region (3.3) also apply to
> using-declarations."
>
> The rules in section 3.3 include the rule in 3.3/4 that says:
>
> "Given a set of declarations in a single declarative region, each of which
> specifies the same unqualified name,
>_- they shall all refer to the same entity, or all refer to functions and
> function templates; or
>_- exactly one declaration shall declare a class name or enumeration name
> that is not a typedef name and the other declarations shall all refer to the
> same object or enumerator, or all refer to functions and function templates;
> in this case the class name or enumeration name is hidden (3.3.7)."
>
> Your example above violates this rule because b) and c) do not refer to the
> same entity.
That's the point.
What is the reason for that b) and c) don't refer to the same entity?
It seems for me that the statement "they SHALL all refer to the same entity,
(or ...)" means:

1) they HAVE TO all refer to the same entity, otherwise the program is
ill-formed, but

2) whether they refer to the same entity or not is determined/guaranteed by
some OTHER rules.

As for function, 13.2/1 says the case when two function declarations of the
same name refer to the same function:

"Two function declarations fo the same name refer to the same function if they
are in the same scope and have equivalent parameter declarations. (...)"

This is an example of "OTHER rules" I say in 2). I can't find such a statement
for objects.
So, about an example "extern int i; extern int i;" either, I can't agree with
what you wrote:
> These two declarations refer to the same object because they refer to the
> same name in the same namespace.
Is this stated in the Standard?

>> why cannot c) be the redeclaration of A::i in B?
>
> Read on to the next paragraph (3.1/2). It explains how to tell if a declaration
> is a definition. You'll see that c) is a definition, and B::i is already
> defined because of the using declaration. That violates the one-definition rule
> (3.2).
Then you mean that b) and c) refer to the same entity, but that violates ODR?
This is the candidate answer 2) I wrote in the first posting.

2) Ill-formed, because the definition of i in B (line c)) is redefinition of i
in A (line a)), which violates ODR. That is, i of c) refers to A::i because of
b).

Or, you mean that such a violation of ODR avoids an interpretation that c) is
redeclaration of A::i?
If so, "int i; int i;" means declarations for two different objects, in order
to avoid a violation of ODR.
Or, suppose the following example:

namespace A {
    extern int i; // not defined
}
namespace B {
    using A::i;   // not defined
    int i;        // doesn't violate ODR
}


If my reading of the word "shall" is not wrong, some statements like 13.2/1
for other entities are needed, I think.

Thank you.


Hideaki Onaru

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Darin Adler <darin@bentspoon.com>
Date: 2000/03/26
Raw View
In article <38DBAB3E.550C89E9@mx1.ttcn.ne.jp>, gomarine@mx1.ttcn.ne.jp
wrote:

> namespace A {
>     int i; // a)
> }
> namespace B {
>     using A::i; // b)
>     int i; // c)
> }

> What is the reason for that b) and c) don't refer to the same entity?
> It seems for me that the statement "they SHALL all refer to the same
> entity, (or ...)" means:
>
> 1) they HAVE TO all refer to the same entity, otherwise the program is
> ill-formed, but
>
> 2) whether they refer to the same entity or not is determined/guaranteed
> by some OTHER rules.

That seems like the correct interpretation of "shall".

> This is an example of "OTHER rules" I say in 2). I can't find such a
> statement for objects.
> So, about an example "extern int i; extern int i;" either, I can't
> agree with what you wrote:

> > These two declarations refer to the same object because they refer to
> > the same name in the same namespace.

> Is this stated in the Standard?

Yes.

In 3.3/1 it says,

"Every name is introduced in some portion of program text called a
declarative region, which is the largest part of the program in which
that name is valid, that is, in which that name may be used as an
unqualified name to refer to the same entity."

When a name is introduced in namespace scope, then the name can be used
as an unqualified name to refer to the same entity. An object is one
kind of entity.

> Then you mean that b) and c) refer to the same entity, but that violates
> ODR?  This is the candidate answer 2) I wrote in the first posting.
>
> 2) Ill-formed, because the definition of i in B (line c)) is
> redefinition of i in A (line a)), which violates ODR. That is, i of
> c) refers to A::i because of b).

I think that is the correct interpretation.

    -- Darin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hideaki Onaru <gomarine@mx1.ttcn.ne.jp>
Date: 2000/03/16
Raw View
Thank you for your reply.

> > For example,
>
> > extern int i;
> > extern int i;
>
> > How does one deduce that these two declarations refer to the "same" object?
>
> The grammar alone doesn't tell you that. These definitions have the same
> grammar as extern int i; extern int j; . Thus the standard covers it by
> an explicit rule, as you noted yourself.
>
> > "A declaration introduces names into a translation unit or redeclares names
> > introduced by previous declarations." (3.1/1)
>
> > Can this be the reasoning for it?
> > If so,
>
> Yes, this applies. The statements above are both declarations.

I feel some gap between my question and your answer, which was maybe caused by
the gap I made when saying "reasoning".
I meant to say:
If, because there is a previous declaration of i, the second declaration of i
is a redeclaration, and if a redeclaration, generally, refers to the same
object as the previous declaration (this assumption is not stated in the Draft
though), the statement in 3.1/1 above can be a reasoning for the fact that two
declarations refer to the same object.
Is this deduction true?

This is what I meant by saying "Can this be the reasoning for it?"
And if this is true,

> > namespace A {
> >     int i;      // a)
> > }
> > namespace B {
> >     using A::i; // b)
> >     int i;      // c)
> > }

it seems to me that also i of c) can be a redeclaration, because there is a
previous declaration of i, that is, b).

> > why cannot c) be the redeclaration of A::i in B?
>
> Because it's a definition. A definition can never be a redeclaration
> ( by definition :-) ).

Why cannot a definition be a redeclaration?

extern int i;
int i;        // a definition which is (maybe) also a redeclaration

> I do wonder myself about:
>
> namespace A {
>         int f() {};     // Definition
> }
> namespace B {
>         int A::f();     // Declaration
> }
>
> I'm not sure exactly what is declared. It it B::A::f(), or B::f() ?

"(...) A declarator-id shall not be qualified except for the definition of a
member function or static member or nested class outside of its class, the
definition or explicit instantiation of a function, variable or class member
of a namespace outside of its namespace, or the definition of a previously
declared explicit specialization outside of its namespace, or the declaration
of a friend function that is a member of another class or namespace. (...)"
(8.3 Meaning of declarators, paragraph 1)

So I think the example you wrote is ill-formed, and the code below

namespace A {
    int f();
}
namespace B {
    int A::f() {}
}

is also ill-formed according to the statement:

"Members of a named namespace can also be defined outside that namespace by
explicit qualification of the name being defined, provided that the entity
being defined was already declared in the namespace and the definition appears
after the point of declaration in a namespace scope that encloses the
declaration's namespace." (7.3.1.2 Namespace member definitions, paragraph 1)

For another example,

namespace A {
    int f();        // 1)
}
namespace B {
    namespace A {
        int f();    // 2)
    }
    int A::f() {}   // definition of f of 2)
    int ::A::f() {} // ill-formed
}

Anyway, it is a declaration / declarations that a name lookup finds.

"(...) Name look up associates the use of a name with a declaration of that
name. (...)" (3.4 Name look up, paragraph 1)

But there are some cases when two or more declarations "refer" to the "same" entity.
So, although each use of a name will be resolved to some declaration of that
name, the grammatical element "declaration" is not good enough to determine
what is the "same" and what is the "different".

    use of a name ---> declaration ---> entity

The Draft requires declarations to refer to the same entity in certain cases:

"Two names that are the same and that are declared in defferent scopes shall
denote the same object, reference, function, type, enumerator, template or
namespace if
- both names have external linkage or else both names have internal linakage
and are declared in the same translation unit; and
- both names refer to members of the same namespace or to members, not by
inheritance, of the same class; and
- when both names denote functions, the function types are identical for the
purpose of overloading; and
- when both names denote function templates, the signatures are the same."
(3.5 Program and linkage, paragraph 9)

"If the set of declarations and using-declarations for a single name are given
in a declarative region,
- they shall all refer to the same entity, or all refer to functions; or
- exactly one declaration shall declare a class name or enumeration name and
the other declarations shall all refer to the same entity or all refer to
functions; in this case the class name or enumeration name is hidden."
(7.3.3 The using declaration, paragraph 10)

Hence in the simple example

extern int i;
extern int i;

two i's have to refer to the same object (otherwise this is ill-formed
program), but, I ask again, how can one examine whether they refer to the same
object or not?

Thank you.


Hideaki Onaru

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hideaki Onaru <gomarine@mx1.ttcn.ne.jp>
Date: 2000/03/09
Raw View
[Moderator's note: this is a repost.  Apologies if you see this twice.]

Suppose the code:

namespace A {
    int i;
}
namespace B {
    int i;
    using A::i;
}

This is ill-formed because of 7.3.3 "The using declaration", paragraph 1:

"(...) A name specified in a using-declaration in a class or namespace scope
shall not already be a member of that scope."

(This quote is from Draft of December, 1996. The sentence might have been changed.)
Then,

namespace A {
    int i;         // a)
}
namespace B {
    using A::i;    // b)
    int i;         // c)
}

is this also ill-formed?

1) Ill-formed, because i of c) doesn't refer to A::i and this is against
7.3.3, paragraph 10:
"If the set of declarations and using-declarations for a single name are given
in a declarative region,
- they shall all refer to the same entity, or all refer to functions; or (...)".
(Maybe this is true? But how can one say that i of c) doesn't refer to A::i?)

2) Ill-formed, because the definition of i in B (line c)) is redifinition of i
in A (line a)), which violates ODR. That is, i of c) refers to A::i because of
b). cf.

namespace B {
    using A::i;
    i = 0;         // this i refers to A::i
}

3) Ill-formed, for other reasons, and it's out of the question which i of c)
refers to, as the first code.

4) No problem yet, because i of c) doesn't refer to A::i and c) is definition
of another i. If i is used in B later, the use causes ambiguity. cf.

namespace B {
    using namespace A;
    int i;         // No problem yet (See example of 7.3.4, paragraph 3)
}

5) Other answers...

My confusion here is related to the questions:
Is a name looked up when it is declared (,not "when it is used")?
(I can't forget that the Draft refers to "lookup" of friend-declared names.)
Or, how can one know which entity a name (in a declaration) refers to?
Or, how can one know whether a set of declarations all refer to the "same" entity?
Or, what's the meaning of "same" entity??

Hideaki Onaru
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Hideaki Onaru <gomarine@mx1.ttcn.ne.jp>
Date: 2000/03/11
Raw View
I am who posted the original article.

> 2) Ill-formed, because the definition of i in B (line c)) is redifinition of i
> in A (line a)), which violates ODR. That is, i of c) refers to A::i because of
> b). cf.
>
> namespace B {
>     using A::i;
>     i = 0;         // this i refers to A::i
> }

I should have written as follows (even if it makes no sense as an example or not):

namespace B {
    using A::i;
    void f() {
        i =0;       // this i refers to A::i
    }
}

Hideaki Onaru

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 2000/03/14
Raw View
Hideaki Onaru <gomarine@mx1.ttcn.ne.jp> writes:

> 1) Ill-formed, because i of c) doesn't refer to A::i and this is against
> 7.3.3, paragraph 10:

> "If the set of declarations and using-declarations for a single name
> are given in a declarative region,
> - they shall all refer to the same entity, or all refer to
> functions; or (...)".

> (Maybe this is true? But how can one say that i of c) doesn't refer to A::i?)


It is ill-formed for essentially that reason. Please note that
7.3.3/10 now reads

# Since a using   declaration is a declaration, the restrictions on
# declarations of the same name in the same declarative region (3.3)
# also apply to using   declarations.

You cannot have two object declarations with the same name in one
scope, hence it is ill-formed

> 2) Ill-formed, because the definition of i in B (line c)) is
> redifinition of i in A (line a)), which violates ODR. That is, i of
> c) refers to A::i because of b). cf.

No, the ODR (one *definition* rule) does not apply; the
using-declaration is a declaration.

> Is a name looked up when it is declared (,not "when it is used")?

Only insofar as to determine whether the conditions of 3.3,
specifically 3.3/4 are met. There is some kind of lookup involved for
other declarations as well. For example, for friend declarations and
template specializations, there is the need to search for a "prior
declaration".

> (I can't forget that the Draft refers to "lookup" of friend-declared
> names.)

It is currently an issue what kind of lookup is used in the case of
friend declarations, see

http://www.informatik.hu-berlin.de/~loewis/corer9.html#138

> Or, how can one know which entity a name (in a declaration) refers to?

I would assume that normal name lookup is used to determine the entity
being referred to in a using-declaration.

Regards,
Martin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Darin Adler <darin@bentspoon.com>
Date: 2000/03/14
Raw View
In article <fjh-000309-162117@cs.mu.oz.au>, gomarine@mx1.ttcn.ne.jp
wrote:

> Suppose the code:
>
>     namespace A {
>         int i;
>     }
>     namespace B {
>         int i;
>         using A::i;
>     }
>
> This is ill-formed because of 7.3.3 "The using declaration", paragraph 1:
>
> "(...) A name specified in a using-declaration in a class or
> namespace scope shall not already be a member of that scope."
>
> (This quote is from Draft of December, 1996. The sentence might have
> been changed.)
>
>     namespace A {
>         int i;         // a)
>     }
>     namespace B {
>         using A::i;    // b)
>         int i;         // c)
>     }
>
> is this also ill-formed?

Yes.

You really must get the final standard and *stop* using an out-of-date
draft. In this case, the section has been completely rewritten since the
draft. The final standard says this in 7.3.3/10:

   "Since a using-declaration is a declaration, the restrictions on
declarations of the same name in the same declarative region (3.3) also
apply to using-declarations."

The rules in section 3.3 include the rule in 3.3/4 that says:

   "Given a set of declarations in a single declarative region, each of
which specifies the same unqualified name,

   _(IQ_(J they shall all refer to the same entity, or all refer to functions
and function templates; or
   _(IQ_(J exactly one declaration shall declare a class name or enumeration
name that is not a typedef name and the other declarations shall all
refer to the same object or enumerator, or all refer to functions and
function templates; in this case the class name or enumeration name is
hidden (3.3.7)."

Your example above violates this rule because b) and c) do not refer to
the same entity.

The rules about how names are looked up and the other questions you are
asking are explained better in the final standard than in the 3-year-old
draft you are working with.

    -- Darin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hideaki Onaru <gomarine@mx1.ttcn.ne.jp>
Date: 2000/03/15
Raw View
Thank you for your reply.

> You cannot have two object declarations with the same name in one
> scope, hence it is ill-formed

The assertion that it is ill-formed is based on that those declarations refer
to different objects.
And the point I don't understand well is the grammatical deduction of the fact
that they refer to different objects.
For example,

extern int i;
extern int i;

How does one deduce that these two declarations refer to the "same" object?

"A declaration introduces names into a translation unit or redeclares names
introduced by previous declarations." (3.1/1)

Can this be the reasoning for it?
If so,

namespace A {
    int i;      // a)
}
namespace B {
    using A::i; // b)
    int i;      // c)
}

why cannot c) be the redeclaration of A::i in B?

"Two names that are the same and that are declared in defferent scopes shall
denote the same object, reference, function, type, enumerator, template or
namespace if
- both names have external linkage or else both names have internal linkage
and are declared in the same translation unit; and
- both names refers to members of the same namespace or to members, not by
inheritance, of the same class; and
(...)" (3.5/9)

A::i is also a member of B by using declaration b), two i's of a) and c) can
be said to be members of the same namespace B, can't they? (If so, i's of b)
and c) can be said to denote the same object...)
Or, at the point of declaration a), the i is not yet a member of B, so this
deduction fails?
(that is, the property "referring to the same object or not" cannot be
determined only by the two names but it needs the positions of declarations?)

I know this must be a quibbling.
But I can't get rid of it...
Please let me know what I've missed.
Thank you.


Hideaki Onaru

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Michiel Salters <salters@lucent.com>
Date: 2000/03/15
Raw View
Hideaki Onaru wrote:

> > You cannot have two object declarations with the same name in one
> > scope, hence it is ill-formed

> The assertion that it is ill-formed is based on that those declarations refer
> to different objects.
> And the point I don't understand well is the grammatical deduction of the fact
> that they refer to different objects.
> For example,

> extern int i;
> extern int i;

> How does one deduce that these two declarations refer to the "same" object?

The grammar alone doesn't tell you that. These definitions have the same
grammar as extern int i; extern int j; . Thus the standard covers it by
an explicit rule, as you noted yourself.

> "A declaration introduces names into a translation unit or redeclares names
> introduced by previous declarations." (3.1/1)

> Can this be the reasoning for it?
> If so,

Yes, this applies. The statements above are both declarations.

> namespace A {
>     int i;      // a)
> }
> namespace B {
>     using A::i; // b)
>     int i;      // c)
> }

> why cannot c) be the redeclaration of A::i in B?

Because it's a definition. A definition can never be a redeclaration
( by definition :-) ).

I do wonder myself about:

namespace A {
 int f() {}; // Definition
}
namespace B {
 int A::f(); // Declaration
}

I'm not sure exactly what is declared. It it B::A::f(), or B::f() ?

> Hideaki Onaru

Michiel Salters

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Darin Adler <darin@bentspoon.com>
Date: 2000/03/16
Raw View
In article <38CE5B7F.3A9E5594@mx1.ttcn.ne.jp>, gomarine@mx1.ttcn.ne.jp
wrote:

> > You cannot have two object declarations with the same name in one
> > scope, hence it is ill-formed

This is a misleading sentence (at least out of context like this).

The actual wording of the rule (called the "one definition rule") says
that, "No translation unit shall contain more than one definition of any
variable, function, class type, enumeration type or template." You are
allowed to declare something multiple times, but not to define something
multiple times.

> The assertion that it is ill-formed is based on that those
> declarations refer to different objects. And the point I don't
> understand well is the grammatical deduction of the fact that they
> refer to different objects. For example,
>
>     extern int i;
>     extern int i;
>
> How does one deduce that these two declarations refer to the "same"
> object?

These two declarations refer to the same object because they refer to
the same name in the same namespace. The following declarations also
refer to the same object:

    int i;
    int i;

But they are illegal because they are two definitions of the same
object, thus they violate the one-definition rule (3.2).

> "A declaration introduces names into a translation unit or redeclares
> names introduced by previous declarations." (3.1/1)
>
> Can this be the reasoning for it?
> If so,
>
>     namespace A {
>         int i;      // a)
>     }
>     namespace B {
>         using A::i; // b)
>         int i;      // c)
>     }
>
> why cannot c) be the redeclaration of A::i in B?

Read on to the next paragraph (3.1/2). It explains how to tell if a
declaration is a definition. You'll see that c) is a definition, and
B::i is already defined because of the using declaration. That violates
the one-definition rule (3.2).

    -- Darin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/03/16
Raw View
In article <darin-264CB3.18085214032000@fullnews.metawire.com>, Darin
Adler <darin@bentspoon.com> writes
>The actual wording of the rule (called the "one definition rule") says
>that, "No translation unit shall contain more than one definition of any
>variable, function, class type, enumeration type or template."
It is stronger than this. "An entity may only be defined once within a
'program'".  Certain things such as class definitions may actually occur
lexically more than once but all versions must be semantically identical
(IOWs any surrounding context must not change the meaning)


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hideaki Onaru <gomarine@mx1.ttcn.ne.jp>
Date: 2000/03/06
Raw View
Suppose the code:

namespace A {
    int i;
}
namespace B {
    int i;
    using A::i;
}

This is ill-formed because of 7.3.3 "The using declaration", paragraph 1:

"(...) A name specified in a using-declaration in a class or namespace scope
shall not already be a member of that scope."

(This quote is from Draft of December, 1996. The sentence might have been changed.)
Then,

namespace A {
    int i;         // a)
}
namespace B {
    using A::i;    // b)
    int i;         // c)
}

is this also ill-formed?

1) Ill-formed, because i of c) doesn't refer to A::i and this is against
7.3.3, paragraph 10:
"If the set of declarations and using-declarations for a single name are given
in a declarative region,
- they shall all refer to the same entity, or all refer to functions; or (...)".
(Maybe this is true? But how can one say that i of c) doesn't refer to A::i?)

2) Ill-formed, because the definition of i in B (line c)) is redifinition of i
in A (line a)), which violates ODR. That is, i of c) refers to A::i because of
b). cf.

namespace B {
    using A::i;
    i = 0;         // this i refers to A::i
}

3) Ill-formed, for other reasons, and it's out of the question which i of c)
refers to, as the first code.

4) No problem yet, because i of c) doesn't refer to A::i and c) is definition
of another i. If i is used in B later, the use causes ambiguity. cf.

namespace B {
    using namespace A;
    int i;         // No problem yet (See example of 7.3.4, paragraph 3)
}

5) Other answers...

My confusion here is related to the questions:
Is a name looked up when it is declared (,not "when it is used")?
(I can't forget that the Draft refers to "lookup" of friend-declared names.)
Or, how can one know which entity a name (in a declaration) refers to?
Or, how can one know whether a set of declarations all refer to the "same" entity?
Or, what's the meaning of "same" entity??

Hideaki Onaru

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]