473,769 Members | 2,134 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 7827
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::Ea tGrass() ).

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::Ea tGrass() ).

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
2988
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 2 : COM objects Tier 3 : SQL Server 2000 DB Flow is : ASP talks with COM objects and COM objects talks with DB Scenario 1
36
16417
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 path with a FSO: Set myFile = Server.CreateObject("Scripting.FileSystemObject").GetFile("\\server2\myshare\myfile.txt") this fails with an Permission Denied. here's the deal:
8
5872
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 namespace'::f(void)" What have I done wrong? Thanks.
5
2464
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
1
1238
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 message Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.
1
1493
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 = "a_comma_delimited_text_file.txt PATH = "C:\folder\subfolder\
0
2266
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 updating the code-behind dll) accessing any aspx page in the application causes the application to run for the first time. Some of the initialization involves reading and writing some text and xml files using simple streamreader and streamwriter...
12
5411
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 variable, but according to the standard there is a difference: "While this is essentially true in practical effect, there are subtle differences. Using static as shown here causes "i" to have internal
22
3934
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 defined entities to the source file in which the anonymous namespace is defined. Entities defined in the anonymous namespace are comparable to C’s static functions and variables. In C++ the static keyword can still be used, but its use is more
0
9589
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5310
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
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
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.