473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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_namespace/header1.hpp>
#include <libs/testing_namespace/header2.hpp>

header2.hpp|5|error: 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 1999
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_namespace/header1.hpp>
#include <libs/testing_namespace/header2.hpp>

header2.hpp|5|error: 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.comwrote:
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_namespace/header1.hpp>
#include <libs/testing_namespace/header2.hpp>
header2.hpp|5|error: 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_namespace::... throughout the
header, that's not practical.
Sep 18 '08 #3
On Sep 18, 5:04 am, Ian Collins <ian-n...@hotmail.comwrote:
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_namespace/header1.hpp>
#include <libs/testing_namespace/header2.hpp>
header2.hpp|5|error: 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_departement_name_head er1.

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 objektorientierter Datenverarbeitung
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...@gmail.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...@gmail.comwrote:
On Sep 18, 5:04 am, Ian Collins <ian-n...@hotmail.comwrote:
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_namespace/header1.hpp>
#include <libs/testing_namespace/header2.hpp>
header2.hpp|5|error: 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_departement_name_head er1.

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 objektorientierter Datenverarbeitung
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
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...
10
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...
29
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...
2
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...
11
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...
1
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...
9
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...
9
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...
13
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.