473,666 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to generate unique namespaces

er
hi,

here's why i'm trying to do:

header1.hpp
namespace{ struct A{};}
struct B1{ A a; };

header2.hpp
namespace{ struct A{};}
struct B2{ A a; };

*.cpp
#include <libs/testing_namespa ce/header1.hpp>
#include <libs/testing_namespa ce/header2.hpp>

header2.hpp|5|e rror: redefinition of ‘struct<unnamed >::A’|

i should expect that because the translation unit includes both
header1 and header 2. so then how can i automatically generate unique
namespaces, one for each of header1 and header2?

thanks.



Sep 18 '08 #1
6 2029
er wrote:
hi,

here's why i'm trying to do:

header1.hpp
namespace{ struct A{};}
struct B1{ A a; };

header2.hpp
namespace{ struct A{};}
struct B2{ A a; };

*.cpp
#include <libs/testing_namespa ce/header1.hpp>
#include <libs/testing_namespa ce/header2.hpp>

header2.hpp|5|e rror: redefinition of ‘struct<unnamed >::A’|

i should expect that because the translation unit includes both
header1 and header 2. so then how can i automatically generate unique
namespaces, one for each of header1 and header2?
You can't. If you must, use a code generator.

If you automatically generate unique namespaces, how do you use them?

--
Ian Collins.
Sep 18 '08 #2
er
On Sep 17, 11:04*pm, Ian Collins <ian-n...@hotmail.co mwrote:
er wrote:
hi,
here's why i'm trying to do:
header1.hpp
namespace{ struct A{};}
struct B1{ A a; };
header2.hpp
namespace{ struct A{};}
struct B2{ A a; };
*.cpp
#include <libs/testing_namespa ce/header1.hpp>
#include <libs/testing_namespa ce/header2.hpp>
header2.hpp|5|e rror: redefinition of ‘struct<unnamed >::A’|
i should expect that because the translation unit includes both
header1 and header 2. so then how can i automatically generate unique
namespaces, one for each of header1 and header2?

You can't. *If you must, use a code generator.

If you automatically generate unique namespaces, how do you use them?

--
Ian Collins.
thanks.

yes... you also have to use the unique_namespac e::... throughout the
header, that's not practical.
Sep 18 '08 #3
On Sep 18, 5:04 am, Ian Collins <ian-n...@hotmail.co mwrote:
er wrote:
here's why i'm trying to do:
header1.hpp
namespace{ struct A{};}
struct B1{ A a; };
header2.hpp
namespace{ struct A{};}
struct B2{ A a; };
*.cpp
#include <libs/testing_namespa ce/header1.hpp>
#include <libs/testing_namespa ce/header2.hpp>
header2.hpp|5|e rror: redefinition of ?struct<unnamed >::A?|
i should expect that because the translation unit includes
both header1 and header 2. so then how can i automatically
generate unique namespaces, one for each of header1 and
header2?
You can't.
For some definition of "unique". Something like:

namespace header1_private { /* ... */ }

is sufficient for a lot of cases. Or for the really paranoid:
corporate_name_ division_name_d epartement_name _header1.

And since nobody has mentionned it: you almost never want an
anonymous namespace in a header. In his case, for example, if
header1.hpp is included in more than one translation unit, he
has undefined behavior, because of a violation of the one
definition rule for B1.
If you must, use a code generator.
Like you do for the include guards?
If you automatically generate unique namespaces, how do you
use them?
As you said, with a code generator:-). As long as everything is
generated by the same code generator, everything is hunky dory.
(This works for include guards because except for the #ifdef and
immediately following #define, you never use the symbol. And
those are automatically inserted by the "code generator" when
you create the file.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 18 '08 #4
How to merge a namespace with another namespace with different header
like C++ did as namespace std consists of many header file?

Thanks.
Sep 18 '08 #5
On Sep 18, 9:08 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
PeterAP...@gmai l.com a écrit :
How to merge a namespace with another namespace with different header
like C++ did as namespace std consists of many header file?

Simply use the same namespace name:

a.h
-------

namespace myspace
{
struct A {};}

-----------------

b.h
-------

namespace myspace
{
struct B {};}

-----------------

myspace::A var_a;
myspace::B var_b;


A billion thanks for your help.
Sep 20 '08 #6
er
On Sep 18, 4:55*am, James Kanze <james.ka...@gm ail.comwrote:
On Sep 18, 5:04 am, Ian Collins <ian-n...@hotmail.co mwrote:
er wrote:
here's why i'm trying to do:
header1.hpp
namespace{ struct A{};}
struct B1{ A a; };
header2.hpp
namespace{ struct A{};}
struct B2{ A a; };
*.cpp
#include <libs/testing_namespa ce/header1.hpp>
#include <libs/testing_namespa ce/header2.hpp>
header2.hpp|5|e rror: redefinition of ?struct<unnamed >::A?|
i should expect that because the translation unit includes
both header1 and header 2. so then how can i automatically
generate unique namespaces, one for each of header1 and
header2?
You can't.

For some definition of "unique". *Something like:

* * namespace header1_private { /* ... */ }
Yep, that is also what I have turned to. Thanks for this and the other
answers.
is sufficient for a lot of cases. *Or for the really paranoid:
corporate_name_ division_name_d epartement_name _header1.

And since nobody has mentionned it: you almost never want an
anonymous namespace in a header. *In his case, for example, if
header1.hpp is included in more than one translation unit, he
has undefined behavior, because of a violation of the one
definition rule for B1.
If you must, use a code generator.

Like you do for the include guards?
If you automatically generate unique namespaces, how do you
use them?

As you said, with a code generator:-). *As long as everything is
generated by the same code generator, everything is hunky dory.
(This works for include guards because except for the #ifdef and
immediately following #define, you never use the symbol. *And
those are automatically inserted by the "code generator" when
you create the file.)

--
James Kanze (GABI Software) * * * * * * email:james.ka. ..@gmail.com
Conseils en informatique orientée objet/
* * * * * * * * * *Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 20 '08 #7

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

Similar topics

2
6925
by: PeterW | last post by:
I have an xml file from which I want to generate an xsd schema and at a later stage a cs class. The xml file has a mix of defined namespaces and also an empty namespace. These are defined as follows: <silcn:silcn xmlns:silcn='http://silcn.org/200309' xmlns='http://xmlprobe.com/200312'> it contains an element <report> off the root and also a separate <Silcn:report> again off the root.
10
18588
by: Mamuninfo | last post by:
Hello, Have any function in the DB2 database that can generate unique id for each string like oracle, mysql,sybase,sqlserver database. In mysql:- select md5(concat_ws("Row name")) from tablename; Here this function generate unique id for each row of the table. Regards..
29
3730
by: Lauren Wilson | last post by:
Does anyone know how the following info is extracted from the user's computer by a Front Page form? HTTP User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0 I only ask because I believe I could use the same info as part of a scheme to generate a unique (or at least less common) serialized id code for the user's computer as part of a software locking and activation system. If I had a DLL...
2
4535
by: Chris Dunaway | last post by:
I have a web service which is accessed by a windows forms application. When the application submits a unit of work (a "job"), I want to return a job receipt or tracking number back to the application. My requirements are these: 1. The receipt number must be unique (like a guid is unique) 2. The receipt must be NON sequential. 3. If possible, I'd like it to be 10 to 12 characters long. 4. If possible, I'd like to include only...
11
4485
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any AutoNumber* fields in them. Correct me if I'm wrong, but I'm assuming this means that I cannot now alter these existing Access tables and change their primary key to an "AutoNumber" type. If I'm right about this, I need some suggestions as to the...
1
3646
by: Daniel Hilgarth | last post by:
Hello, I am currently trying to use XSLT for the creation of multiple HTML-files from a single XML-File. This HTML-files need to have links to each other. The following information might be important: There are some special nodes that will start a new HTML-page ("page-nodes"). Those nodes can be nested. Those nodes have an attribute "name" which is not necessarily unique. There are another special nodes that will create a link in one...
9
6449
by: Omatase | last post by:
I have a set of about 6 or so strings that I need to use to generate a unique hash. This hash will become the unique key in a database so the hash has to be the same each time I gen it for any 1 set of strings. Is there something out there that already does this written in javascript? I didn't find anything doing a google search.
9
18111
by: Robert Mago | last post by:
Is there a way to create a 10 characthers or less, alph-numeric string which is unique. I can't use the guid since its longer then 10 characthers. Also i cannot use a random number, since being random does not mean that its unique.
13
40555
by: gamernaveen | last post by:
I am coding a script , where basically the user has to enter his name , choose file , file comments if required and upload. The file comments , name , filename will be stored in the database with auto_increment id. The file gets uploaded to the server too. The problem is I want to generate a 10 digit unique code , which makes it simple for the user to download the file , he just has to enter the 10-digit code , and my script will deliver the...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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
8550
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
7385
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...
1
6192
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5663
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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

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.