473,545 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ISO/IEC 14882 §3.3 friend, extern and scope

This note appears in the discussion of name hiding and uniqueness:

§3.3 #4[Note: these restrictions apply to the declarative region into which
a name is introduced, which is not necessarily the same as the region in
which the declaration occurs. In particular, elaborated-type-specifiers
(3.3.1) and friend declarations (11.4) may introduce a (possibly not
visible) name into an enclosing namespace; these restrictions apply to that
region. Local extern declarations (3.5) may introduce a name into the
declarative region where the declaration appears and also introduce a
(possibly not visible) name into an enclosing namespace; these restrictions
apply to both regions.]

This note is item #6 in the discussion of "Point of declaration"
§3.3.1 #6[Note: friend declarations refer to functions or classes that are
members of the nearest enclosing namespace, but they do not introduce new
names into that namespace (7.3.1.2). Function declarations at block scope
and object declarations with the extern specifier at block scope refer to
declarations that are members of an enclosing namespace, but they do not
introduce new names into that scope. ]

What exactly do these statements mean?

"[E]laborated-type-specifiers and friend declarations may introduce a
(possibly not visible) name into an enclosing namespace;"

"Local extern declarations may introduce a name into the declarative region
where the declaration appears and also introduce a (possibly not visible)
name into an enclosing namespace;"

"Friend declarations refer to functions or classes that are members of the
nearest enclosing namespace, but they do not introduce new names into that
namespace"

"[O]bject declarations with the extern specifier at block scope refer to
declarations that are members of an enclosing namespace, but they do not
introduce new names into that scope."

Not only do I not understand their basic meaning, they seem contradictory to
me. The part about "elaborated-type-specifiers" I take to mean "forward
declarations", and the statement appears to mean that bar is introduced
into the namespace foo in this example:

namespace foo{ class bar; }

But what is it saying the friend declaration is introducing into the
namespace foo in this example?

namespace foo{ struct bar{ friend void baz(int i); }; }

The same goes for extern?

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 7 '06 #1
5 1814
Steven T. Hatton wrote:
This note appears in the discussion of name hiding and uniqueness:
....
This note is item #6 in the discussion of "Point of declaration"
§3.3.1 #6[Note: friend declarations refer to functions or classes that are
members of the nearest enclosing namespace, but they do not introduce new
names into that namespace (7.3.1.2). Function declarations at block scope
and object declarations with the extern specifier at block scope refer to
declarations that are members of an enclosing namespace, but they do not
introduce new names into that scope. ]

What exactly do these statements mean?
struct A
{
friend struct B;
};

B * b;

The code above is not valid.

So the friend declaration for B does not introduce a struct B but it
does point to it. This means that the code above is invalid.
struct A
{
friend struct B;
};

struct B;
B * b;

This code is now valid.

The same goes for extern?
No.
Dec 8 '06 #2
Gianni Mariani wrote:
Steven T. Hatton wrote:
>This note appears in the discussion of name hiding and uniqueness:
...
>This note is item #6 in the discussion of "Point of declaration"
§3.3.1 #6[Note: friend declarations refer to functions or classes that
are members of the nearest enclosing namespace, but they do not introduce
new names into that namespace (7.3.1.2). Function declarations at block
scope and object declarations with the extern specifier at block scope
refer to declarations that are members of an enclosing namespace, but
they do not introduce new names into that scope. ]

What exactly do these statements mean?

struct A
{
friend struct B;
};

B * b;

The code above is not valid.

So the friend declaration for B does not introduce a struct B but it
does point to it. This means that the code above is invalid.
struct A
{
friend struct B;
};

struct B;
B * b;

This code is now valid.
I get it. If the declaration of B were a definition - which could never be
the case for an object such as B - then that definition would introduce B
into the enclosing namespace. Right?
>
>The same goes for extern?

No.
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 8 '06 #3
Steven T. Hatton wrote:
....
>
I get it. If the declaration of B were a definition - which could never be
the case for an object such as B - then that definition would introduce B
into the enclosing namespace. Right?
I think your terminology of definition and declaration is wrong.

struct B; // declaration

struct B {}; // definition

friend struct B; // "friend declaration"
If we use the teminology from the standard:

"friend struct B" does not "introduce" "struct B" into the namespace.

"struct B" does introduce itself into the namespace, however, that
exactly what it's purpose is so you would expect it to.
Dec 8 '06 #4
Gianni Mariani wrote:
Steven T. Hatton wrote:
...
>>
I get it. If the declaration of B were a definition - which could never
be the case for an object such as B - then that definition would
introduce B
into the enclosing namespace. Right?

I think your terminology of definition and declaration is wrong.
I should have said "object type" such as B.
#include <iostream>
namespace foo {

struct Bar {
Bar():_splat("S PLAT!"){}
friend const char* A(const Bar& b);
friend const char* B(const Bar& b)
{ return b._splat; }//B is defined, but B is not an object type.
private:
const char* _splat;
};

//Is A here, yet "possibly not visible"?

void baz () {
Bar bar;
std::cout<<B(ba r)<<std::endl;
//B is visible in the enclosing Bar's enclosing scope.
}

}

int main(){
foo::baz();
}
struct B; // declaration

struct B {}; // definition

friend struct B; // "friend declaration"
If we use the teminology from the standard:

"friend struct B" does not "introduce" "struct B" into the namespace.
Does it? §3.3/4 "friend declarations (11.4) may introduce a (possibly not
visible) name into an enclosing namespace;"

I am pretty sure that is an error in the Standard. I cannot reconcile it
with the following unless I allow that the declaration may also be a
definition:

§3.3 "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. In general, each particular name is valid
only within some possibly discontiguous portion of program text called its
scope."

§3.3.7/5 "If a name is in scope and is not hidden it is said to be visible."

If I do allow that a declaration may also be a definition, then the
following appears to be in error:

§3.3.1 #6"friend declarations refer to functions or classes that are members
of the nearest enclosing namespace, but they do not introduce new
names into that namespace."

To be honest, the more closely I read clause 3, the more I realize that it
contains several subtle errors.
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 14 '06 #5
Steven T. Hatton wrote:
Gianni Mariani wrote:
>Steven T. Hatton wrote:
...
>>>
I get it. If the declaration of B were a definition - which could never
be the case for an object such as B - then that definition would
introduce B
into the enclosing namespace. Right?

I think your terminology of definition and declaration is wrong.

I should have said "object type" such as B.
#include <iostream>
namespace foo {

struct Bar {
Bar():_splat("S PLAT!"){}
friend const char* A(const Bar& b);
friend const char* B(const Bar& b)
{ return b._splat; }//B is defined, but B is not an object type.
private:
const char* _splat;
};

//Is A here, yet "possibly not visible"?

void baz () {
Bar bar;
std::cout<<B(ba r)<<std::endl;
//B is visible in the enclosing Bar's enclosing scope.
}

}

int main(){
foo::baz();
}
>struct B; // declaration

struct B {}; // definition

friend struct B; // "friend declaration"
If we use the teminology from the standard:

"friend struct B" does not "introduce" "struct B" into the namespace.

Does it? §3.3/4 "friend declarations (11.4) may introduce a (possibly not
visible) name into an enclosing namespace;"

I am pretty sure that is an error in the Standard. I cannot reconcile it
with the following unless I allow that the declaration may also be a
definition:

§3.3 "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. In general, each particular name is
valid only within some possibly discontiguous portion of program text
called its scope."

§3.3.7/5 "If a name is in scope and is not hidden it is said to be
visible."

If I do allow that a declaration may also be a definition, then the
following appears to be in error:

§3.3.1 #6"friend declarations refer to functions or classes that are
members of the nearest enclosing namespace, but they do not introduce new
names into that namespace."

To be honest, the more closely I read clause 3, the more I realize that it
contains several subtle errors.
Ha! I finally realized why everybody else is confused about my confusion.
I misunderstood the problem wrong! I still believe the wording in the
Standard is wrong, but now I believe it is wrong in more ways than I
thought. I wasn't expected to consider a definition to be a declaration
(even though it is). I was just supposed to be confused about the fact
that a name is both introduced and not introduced into a namespace at the
same time.

This make a who lot better nonsense than the other examples I posted:

#include <iostream>

namespace other {
struct D {
D():_data("D::_ data"){}
const char* _data;
virtual void fun() const { std::cout<<"D:: fun():"<<_data< <std::endl; }
};
void pal(const D& d){
std::cout<<"oth er::pal:"<<d._d ata<<std::endl;
d.fun();
}
}

namespace foo {
using other::D;
struct Bar: D {
Bar():_splat("S PLAT!"){}

friend void pal(const D& d);
friend const char* B(const Bar& b) { return b._splat; }
friend void chum(const Bar& b) { std::cout<<"chu m
of "<<b._data<<std ::endl; }

private:
const char* _splat;
virtual void fun() const { std::cout<<"Bar ::fun():"<<_dat a<<std::endl; }

};
void baz () {
Bar* bar_ptr(new Bar);
std::cout<<B(*b ar_ptr)<<std::e ndl;
other::pal(*bar _ptr);//Why do I need to qualify this?
chum(*bar_ptr);
delete bar_ptr;
}
}

int main(){
foo::baz();
}

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
4260
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except where it's defined.
12
2700
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external linkage? Not much on this in the FAQ or tutorials.
19
3825
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the...
5
16596
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining extern storage class: /****************************************************************** SOURCE FILE ONE...
17
4909
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
5
2844
by: Christian Christmann | last post by:
Hi, I've tree questions on the storage class specifier "extern": 1) Code example: int main( void ) { int b = -2; // my line 3 if ( a ) {
7
2170
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
1
144
by: James Kanze | last post by:
On Apr 11, 3:20 am, Ian Collins <ian-n...@hotmail.comwrote: Yes. ADL kicks in, and finds the function declared in scope wurst::foo.
1
3749
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: - "function prototype scope" for structures and unions? - "extern" for internal linkage ?
0
7487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.