Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using a class in an unnamed namespace as friend

Reply
Thread Tools

using a class in an unnamed namespace as friend

 
 
marco_segurini
Guest
Posts: n/a
 
      06-15-2004
Hi,

the following test program shows a solution to a problem I have had.

Now, this test program is compiled and linked by VS2003 and g++
while Comeau-on-line-compiler fails with this messages:

"ComeauTest.c", line 21: error: constant "COuter::ID" is inaccessible
int i = COuter::ID;
^
....some warning

1 error detected in the compilation of "ComeauTest.c".

who is wrong?

many thanks.
Marco.

//sample begin
namespace
{
class CInner;
}

class COuter
{
friend class CInner;

enum { ID = 5 };
};

namespace
{

class CInner
{
public:
CInner()
{
int i = COuter::ID;
}
};

} //!namespace

int main()
{
return 0;
}
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      06-15-2004
marco_segurini wrote in
news:(E-Mail Removed) m in comp.lang.c++:

> Hi,
>
> the following test program shows a solution to a problem I have had.
>
> Now, this test program is compiled and linked by VS2003 and g++
> while Comeau-on-line-compiler fails with this messages:
>
> "ComeauTest.c", line 21: error: constant "COuter::ID" is inaccessible
> int i = COuter::ID;
> ^
> ...some warning


It would be unusual for Comeau/EDG to be wrong.

>
> 1 error detected in the compilation of "ComeauTest.c".
>
> who is wrong?


VC++ and g++

>


> //sample begin
> namespace
> {
> class CInner;
> }
>
> class COuter
> {
> friend class CInner;
>
> enum { ID = 5 };
> };


Every things fine so far.

>
> namespace
> {
>


Ouch - This isn't the *same* nameless namespace defined above,
its a brand new one.

> class CInner
> {


Double Ouch - Not the the same CInner, this one
*isn't* a friend to COuter.

> public:
> CInner()
> {
> int i = COuter::ID;
> }
> };
>
> } //!namespace
>
> int main()
> {
> return 0;
> }
>


In short you can't pre-*declare* anonymous things.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      06-16-2004
On 15 Jun 2004 19:25:38 GMT, Rob Williscroft <(E-Mail Removed)>
wrote:

>>
>> namespace
>> {
>>

>
> Ouch - This isn't the *same* nameless namespace defined above,
> its a brand new one.


Are you sure? My reading of 7.3.1.1/1 suggests there is only one
anonymous namespace name per translation unit. e.g. the OPs code
should be "translated" to:

//sample begin
namespace unique
{
}
using namespace unique;
namespace unique
{
class CInner;
}

class COuter
{
friend class CInner;

enum { ID = 5 };
};

namespace unique
{

class CInner
{
public:
CInner()
{
int i = COuter::ID;
}
};

} //!namespace

int main()
{
return 0;
}

Interestingly, Comeau fails to compile the above too, and I think this
is an EDG bug (that also causes the failure of the original code). The
friend declaration in COuter should find unique::CInner according to
the name lookup rules for elaborated-type-specifiers. However, Comeau
considers the friend declaration to refer to ::CInner. My analysis may
be flawed of course...

>In short you can't pre-*declare* anonymous things.


But you can according to Comeau. e.g.

namespace
{
extern int i;
}

int main()
{
i = 10; //expect linker error if you can't
}

namespace
{
int i = 1;
}

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      06-16-2004
tom_usenet wrote in news(E-Mail Removed) in
comp.lang.c++:

>> Ouch - This isn't the *same* nameless namespace defined above,
>> its a brand new one.

>
> Are you sure?


I was, but not any more.

> My reading of 7.3.1.1/1 suggests there is only one
> anonymous namespace name per translation unit.
>


I miss-remembered the conclusion of a thread on comp.std.c++.
I should have checked the standard myself of course.

Thanks for the correction.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      06-16-2004
tom_usenet wrote in news(E-Mail Removed) in
comp.lang.c++:

> //sample begin
> namespace unique
> {
> }
> using namespace unique;
> namespace unique
> {
> class CInner;
> }
>
> class COuter
> {
> friend class CInner;
>
> enum { ID = 5 };
> };
>
> namespace unique
> {
>
> class CInner
> {
> public:
> CInner()
> {
> int i = COuter::ID;
> }
> };
>
> } //!namespace
>
> int main()
> {
> return 0;
> }
>
> Interestingly, Comeau fails to compile the above too, and I think this
> is an EDG bug (that also causes the failure of the original code). The
> friend declaration in COuter should find unique::CInner according to
> the name lookup rules for elaborated-type-specifiers. However, Comeau
> considers the friend declaration to refer to ::CInner. My analysis may
> be flawed of course...
>


Seems right. Though ::CInner should refer to unique::CInner in
any case, so the friend declaration is unconditionaly inserting
a declaration for class CInner into ::.

The thread ( http://tinyurl.com/26dqo ) that I miss-remembered
contains this workaround (http://tinyurl.com/2ams9 ):

//sample begin
#define unique
//namespace unique {} using namespace unique;
namespace unique
{
namespace foo
{
class CInner;
}
}

class COuter
{
friend class foo::CInner;

/* friend class ::CInner; doesn't work.
g++ 3.4 and VC++ 7.1 accept it.
*/
enum { ID = 5 };
};

namespace unique
{
namespace foo
{
class CInner
{
public:
CInner()
{
int i = COuter::ID;
}
};
}
} //!namespace

int main()
{
return 0;
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using-declaration or using-directive inside unnamed-namespace? Niels Dekker - no reply address C++ 1 04-27-2010 04:16 PM
unnamed namespace and friend method John Ratliff C++ 9 02-08-2008 04:17 PM
friend and unnamed namespace Ivan Mascovich C++ 9 03-03-2006 01:53 PM
unnamed namespace and friend marco_segurini C++ 1 02-03-2005 02:48 PM
C++ programmers! How do you use unnamed 'namespace's ? Razmig K C++ 3 09-05-2003 06:52 AM



Advertisments