473,511 Members | 14,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing anonymous namespace


Back in the day, if you wanted a function to be self-contained within a
translation unit, you defined the function as "static".

If there were an external linkage function by the same name residing in a
different translation unit, then the current translation unit was simply
oblivious to it and had no way of accessing it. Any time the function
name was mentioned in the current translation unit, it referred to the
"static" one which resides in the current translation.

Here's an example to demonstrate what I mean:

/* a.cpp */

int Func()
{
return 10;
}
/* b.cpp */

static int Func()
{
return 5;
}

int main()
{
Func(); /* This calls the internal function */

/* We have no way of accessing the external
linkage function. */
}
Also note that you can't put a forward declaration in "b.cpp" to the
effect of:

extern int Func();

(This gives a compile error on my system when followed by a static
function of the same name and signature.)

Now that we've moved on to anonymous namespaces, it seems that things
have changed a little. We can now access the external linkage function,
but at expense of not being able to access the internal linkage one (or
so I think?)

Here's the code:
/* a.cpp */

int Func()
{
return 10;
}
/* b.cpp */

int Func(); /* Grants us access to the external one */

namespace {

int Func()
{
return 5;
}

}
int main()
{
::Func(); /* Calls the external one*/

/* I don't think we've any way of calling
the internal one. */
}
If we omit the forward-declaration of "Func" at the beginning of "b.cpp",
then of course, we only have access to the internal one, and NOT to the
external one.
Any thoughts on this?
--

Frederick Gotham
Jun 30 '06 #1
3 7753
Frederick Gotham <fg*******@SPAM.com> wrote:
int Func(); /* Grants us access to the external one */

namespace {

int Func()
{
return 5;
}

}
int main()
{
::Func(); /* Calls the external one*/

/* I don't think we've any way of calling
the internal one. */
}
Any thoughts on this?


Yes. The contents of the nameless namespace are local to
one sourcefile, so whoever's coding that file is free to
choose whatever identifiers they like for names within that
namespace, and so is always free to use ones that do not
conflict with their own code.

So there doesn't seem to be much of a need for a mechanism
to get around this deficiency.

Steve
Jun 30 '06 #2
Steve Pope posted:

int main()
{
::Func(); /* Calls the external one*/

/* I don't think we've any way of calling
the internal one. */
}

Any thoughts on this?


Yes. The contents of the nameless namespace are local to
one sourcefile, so whoever's coding that file is free to
choose whatever identifiers they like for names within that
namespace, and so is always free to use ones that do not
conflict with their own code.

So there doesn't seem to be much of a need for a mechanism
to get around this deficiency.

Imagine this:

We have a load of header files pertaining to a particular library.
Everything in the library is defined in the global namespace (rather than
something like LibraryName::EatGrass() ).

In one of our own source files, we have a function within an anonymous
namespace called "EatGrass". This particular source file also happens to
contain "main". Here's how it looks at the moment:

namespace {

int EatGrass()
{
return 5;
}

}

int main()
{
EatGrass();
}
Later, we decide that we need something from the library, so we include a
header:

#include <somelibrary/consumption.hpp>

namespace {

int EatGrass()
{
return 5;
}

}

int main()
{
EatGrass();
}
Unfortunately now, we get a compile error because the function call is
ambiguous. Normally we'd rememdy this by placing the calling function
within the anonymous namespace also... but alas, the following give us a
linker error:
#include <somelibrary/consumption.hpp>

namespace {

int EatGrass()
{
return 5;
}

int main()
{
EatGrass(); /* Intends to call internal func */
}

}

int SomeOtherFunc()
{
EatGrass(); /* Intends to call external func */
}
Because we can't put "main" inside an anonymous namespace.
--

Frederick Gotham
Jun 30 '06 #3
Frederick Gotham wrote:
Steve Pope posted:

int main()
{
::Func(); /* Calls the external one*/

/* I don't think we've any way of calling
the internal one. */
}

Any thoughts on this?


Yes. The contents of the nameless namespace are local to
one sourcefile, so whoever's coding that file is free to
choose whatever identifiers they like for names within that
namespace, and so is always free to use ones that do not
conflict with their own code.

So there doesn't seem to be much of a need for a mechanism
to get around this deficiency.

Imagine this:

We have a load of header files pertaining to a particular library.
Everything in the library is defined in the global namespace (rather than
something like LibraryName::EatGrass() ).

In one of our own source files, we have a function within an anonymous
namespace called "EatGrass". This particular source file also happens to
contain "main". Here's how it looks at the moment:

namespace {

int EatGrass()
{
return 5;
}

}

int main()
{
EatGrass();
}
Later, we decide that we need something from the library, so we include a
header:

#include <somelibrary/consumption.hpp>

namespace {

int EatGrass()
{
return 5;
}

}

int main()
{
EatGrass();
}
Unfortunately now, we get a compile error because the function call is
ambiguous. Normally we'd rememdy this by placing the calling function
within the anonymous namespace also... but alas, the following give us a
linker error:
#include <somelibrary/consumption.hpp>

namespace {

int EatGrass()
{
return 5;
}

int main()
{
EatGrass(); /* Intends to call internal func */
}

}

int SomeOtherFunc()
{
EatGrass(); /* Intends to call external func */
}
Because we can't put "main" inside an anonymous namespace.


The solution that Steve was thinking about is to simply rename the internal
EatGrass() to something else. That should be easy, because it is only used
in one file.
Another solution would be to put a named namespace into your anonymous one.

Jul 1 '06 #4

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

Similar topics

4
2976
by: Jaydeep | last post by:
Hello, I am facing a strange problem. Problem accessing remote database from ASP using COM+ server application having VB components (ActiveX DLL) installed. Tier 1 : ASP front End (IIS 5.0) Tire...
36
16316
by: Thomas | last post by:
after spending countless hours trying, i give up and hope to get some help in here. on server1 i got the web myweb.com with my test.asp. in the test.asp, i'm trying to read a file from an UNC...
8
5856
by: Jason Heyes | last post by:
I wrote: namespace { void f(); } void f() { std::cout << "hello world" << std::endl; } When I compile, I receive this error: unresolved external symbol "bool __cdecl `anonymous...
5
2451
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
1
1219
by: Paul | last post by:
Hi My web page is accessing a SQL DB to display some data. It works fine using login/password security on the sql server database, but I'm trying to use WIndows authenication and I get the...
1
1479
by: Yama | last post by:
Hi Is there a way using an Intranet ASP.NET web application to access a text file from a specified folder The Logic ReadTextFile.asp ------------------- FILE =...
0
2242
by: Joergen Bech | last post by:
Fairly new to ASP.NET 1.1. Getting the error below when running application on a web server outside of my control, but only the first time I run it: 1. After a long period of inactivity (or...
12
5381
by: Taras_96 | last post by:
Hi everyone, AFAIK external linkage allows you to refer to variables/functions outside of the current translation unit. A variable in an unnamed namespace is similar to declaring a static...
22
3883
by: Luna Moon | last post by:
I am reading the book "C++ Annotations", and here is a quote from the book: Namespaces can be defined without a name. Such a namespace is anonymous and it restricts the visibility of the...
0
7252
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
7153
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...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
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...
1
5077
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
4743
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...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
452
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.