473,508 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using a class in an unnamed namespace as friend

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;
}
Jul 22 '05 #1
4 3793
marco_segurini wrote in
news:a3*************************@posting.google.co 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/
Jul 22 '05 #2
On 15 Jun 2004 19:25:38 GMT, Rob Williscroft <rt*@freenet.co.uk>
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
Jul 22 '05 #3
tom_usenet wrote in news:o8********************************@4ax.com 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/
Jul 22 '05 #4
tom_usenet wrote in news:o8********************************@4ax.com 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/
Jul 22 '05 #5

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

Similar topics

21
8550
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
8
4357
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
16
2953
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
1
2150
by: Marco Jez | last post by:
What are the requirements imposed by the Standard on unnamed namespaces? I mean, is the hidden name guaranteed to be unique across multiple translation units, or only within the declaring...
3
2338
by: Sandy | last post by:
Hi, I have two files as folllows file1.cpp #include<iostream> using namespace std; namespace { void show(); void fun() { cout<<"fun called\n"; } }
12
3767
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
30
4064
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
3
2265
by: CrazyJohn | last post by:
Hi guys, This is my first time posting question here, if I break any rules, please kindly point out. And I'm really glad to be a part of this community. Here is my question, Our lecturer...
3
3420
by: Al Grant | last post by:
Consider two translation units, (1): namespace { struct S { }; } struct D: S { virtual int f(void); }; and (2): #include <typeinfo> struct D; char const *f(D *p) { return...
0
7226
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,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7049
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...
0
7499
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...
0
5631
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,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4709
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...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
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...

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.