Friend Functions and Scope

N

Neelesh

I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
where it is mentioned that
Like a member function, a friend declaration doesnot introduce a name
into an enclosing space. For example :

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope

What I observe is a bit different - it is fine that there is an error
for Xform, but the reason is not that Xform is not in scope - the
reason is that the class's size is not known.
Also, the second line is compiling fine - without any problems. In
other words, I find that the declaration of a friend function or a
class inside a class _does_ bring the name into the scope.

Can somebody explain where I am going wrong?
 
J

Jonathan Mcdougall

Neelesh said:
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
where it is mentioned that
Like a member function, a friend declaration doesnot introduce a name
into an enclosing space. For example :

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope

What I observe is a bit different - it is fine that there is an error
for Xform, but the reason is not that Xform is not in scope - the
reason is that the class's size is not known.

No.

Xform *x;

is also illegal. The name Xfrom does not exist here.
Also, the second line is compiling fine - without any problems.

Well it shouldn't. It's ill-formed.
In
other words, I find that the declaration of a friend function or a
class inside a class _does_ bring the name into the scope.

Can somebody explain where I am going wrong?

Dunno. What compiler are you using? What's the exact code you are
compiling?


Jonathan
 
S

Sumit Rajan

Neelesh said:
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
where it is mentioned that
Like a member function, a friend declaration doesnot introduce a name
into an enclosing space.
True.

For example :

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope



What I observe is a bit different - it is fine that there is an error
for Xform, but the reason is not that Xform is not in scope - the
reason is that the class's size is not known.

You are right about the fact that the class's size is unknown.

However, consider this:
class Matrix {
friend class XForm;
} ;

XForm* x; // error, no Xform in scope

Here, the class's size is *not* required to be known. However there will be
a compile time error saying something to the effect of "unknown identifier"
since there is no XForm in scope.
Also, the second line is compiling fine - without any problems. In

This is the error I get with the same code you posted:

"test.cpp", line 9: error: identifier "invert" is undefined
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope

Regards,
Sumit.
 
N

Neelesh

Jonathan said:
Dunno. What compiler are you using? What's the exact code you are
compiling?

The exact code (word to word, copied and pasted from (DevC++) is

/********** start *************************/

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope

int main()
{
return 0;
}

/********* end ***************************/

The compilers used are g++(3.4.2) and VC++6.0.
For g++, options used are -Wall -Werror -pedantic -ansi
In both cases the code is compiling properly without a single warning
or error.
Also tried with DevC++ (I guess its quite an old version, 4.9.8.0) and
it gives a warning that invert is not defined - but there is no error.
 
N

Neelesh

Neelesh said:
In both cases the code is compiling properly without a single warning
or error.
Also tried with DevC++ (I guess its quite an old version, 4.9.8.0) and
it gives a warning that invert is not defined - but there is no error.

Oh, I just forgot to mention one (obvious) point that I am talking
only about the "compiler errors". It is clear that there will be linker
errors since the function is not defined. But that is not an issue.
 
J

Jonathan Mcdougall

Neelesh said:
The exact code (word to word, copied and pasted from (DevC++) is

/********** start *************************/

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope

int main()
{
return 0;
}

/********* end ***************************/

The compilers used are g++(3.4.2) and VC++6.0.

This was found as a bug in g++, it was deprecated in 3.4, but I don't
know the current status.
For g++, options used are -Wall -Werror -pedantic -ansi
In both cases the code is compiling properly without a single warning
or error.
Also tried with DevC++ (I guess its quite an old version, 4.9.8.0) and
it gives a warning that invert is not defined - but there is no error.

You are working with old compilers. This syntax was accepted before the
standard so old compilers (<1998) are not aware of the changes.

You can be assured that the given program is ill-formed. In doubt,
always check at http://www.comeaucomputing.com/tryitout/.


Jonathan
 
S

shikn

I think "declaration of a friend function " is not a declaration of the
function.
You declear friend funciton just for the class Matrix . but when you refers
to it, the compiler should find a declaration of the function.

Neelesh said:
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
where it is mentioned that
Like a member function, a friend declaration doesnot introduce a name
into an enclosing space. For example :

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope

What I observe is a bit different - it is fine that there is an error
for Xform, but the reason is not that Xform is not in scope - the
reason is that the class's size is not known.
Also, the second line is compiling fine - without any problems. In
other words, I find that the declaration of a friend function or a
class inside a class _does_ bring the name into the scope.

Can somebody explain where I am going wrong?
 
N

Neelesh

This was found as a bug in g++, it was deprecated in 3.4, but I don't
know the current status.

You can be assured that the given program is ill-formed. In doubt,
always check at http://www.comeaucomputing.com/tryitout/.

That solves my doubt. Thanks a lot.
BTW, I tested it with g++ 4.0.0 and the bug still exists.
The code is also compilable on HP-UX's native compiler (aCC)
However, VisualAge 6.0 for AIX gives a compiler error saying that
invert is undefined.
 
S

Sumit Rajan

/********** start *************************/

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope

By the way, did you realize that "Xform" above is different from "XForm"
earlier (note the upper case "F"). Not that it matters in this case since
Xform and XForm should cause the compiler to complain.

Regards,
Sumit.
 
N

Neelesh

Sumit said:
By the way, did you realize that "Xform" above is different from "XForm"
earlier (note the upper case "F"). Not that it matters in this case since
Xform and XForm should cause the compiler to complain.
Ahh! Thanks for pointing that out. It didnot matter becuase that line
was commented. But yes, a valid point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top