Prev: Deleting a singleton object
Next: How can I alter how a user-defined type/enum is displayed? (facet/locale?)
From: Goran on 2 Jul 2010 06:23 On Jul 1, 4:54 pm, "Martin B." <0xCDCDC...(a)gmx.at> wrote: > Is an anonymous / unnamed namespace unique to the translation unit or is > it simply unique, that is, is the following legal: > > *** file.cpp *** > // ... > namespace { > int HelperFn(double d);}; > > // ... > void ExternalFn() { > // ... > int x = HelperFn(1.0); > // ...} > > // ... > namespace { // Is this the *same* anonymous namespace as above? > int HelperFn(double d) { > // ... > return l; > }}; > > *** *** > > It works on VC++2005 but is it std behaviour? Dunno about a standard, but e.g. GCC 3.4.5, MS ccompiler from VS 2008 and comeau online don't like it. HelperFn is a doubly-defined symbol for all three. Bug in VC2005? Goran. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ric Parkin on 2 Jul 2010 09:05 On 1 July, 15:54, "Martin B." <0xCDCDC...(a)gmx.at> wrote: > Is an anonymous / unnamed namespace unique to the translation unit or is > it simply unique, that is, is the following legal: > > *** file.cpp *** > // ... > namespace { > int HelperFn(double d);}; > } > namespace { // Is this the *same* anonymous namespace as above? > int HelperFn(double d) { > // ... > return l; > }}; > It works on VC++2005 but is it std behaviour? Yes, each translation unit has a single unnamed namespace, unique to that TU See 7.3.1.1 where it says it's equiv to namespace unique {} using namespace unique; namespace unique { ... } where all uses of unique in a translation unit are replaced by the same identifier, and this identifier differes from all others in the entire programme. ie even though uses the term "unnamed" the standard actually says it has a name, it's just that you don't know what it is so can't use it. Ric -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: CornedBee on 2 Jul 2010 09:05 On Jul 1, 4:54 pm, "Martin B." <0xCDCDC...(a)gmx.at> wrote: > Is an anonymous / unnamed namespace unique to the translation unit or is > it simply unique Unique to the translation unit, see [namespace.unnamed]p1: "all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the entire program" Sebastian -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Nick Hounsome on 3 Jul 2010 07:55 On 2 July, 21:54, Daniel Kr�gler <daniel.krueg...(a)googlemail.com> wrote: > On 1 Jul., 16:54, "Martin B." <0xCDCDC...(a)gmx.at> wrote: > > *** file.cpp *** > > // ... > > namespace { > > int HelperFn(double d);}; > > > // ... > > void ExternalFn() { > > // ... > > int x = HelperFn(1.0); > > // ...} > > > // ... > > namespace { // Is this the *same* anonymous namespace as above? > > int HelperFn(double d) { > > // ... > > return l; > > }}; > The C++ standard guarantees that *all* occurences > of the same unnamed namespace within the same > translation unit do have the same identifier, see > C++03, 7.3.1.1 [namespace.unnamed]/1: I knew this but it had never occurred to me to use it to forward declare local functions so that I could put the definitions at the end of the file after the more important public stuff. I've been using static for that all these years. Doh! -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Francis Glassborow on 3 Jul 2010 17:40 Ric Parkin wrote: > On 1 July, 15:54, "Martin B." <0xCDCDC...(a)gmx.at> wrote: >> Is an anonymous / unnamed namespace unique to the translation unit or is >> it simply unique, that is, is the following legal: >> >> *** file.cpp *** >> // ... >> namespace { >> int HelperFn(double d);}; >> } >> namespace { // Is this the *same* anonymous namespace as above? >> int HelperFn(double d) { >> // ... >> return l; >> }}; >> It works on VC++2005 but is it std behaviour? > > Yes, each translation unit has a single unnamed namespace, unique to > that TU > > See 7.3.1.1 where it says it's equiv to > > namespace unique {} > using namespace unique; > namespace unique { ... } > > where all uses of unique in a translation unit are replaced by the > same identifier, and this identifier differes from all others in the > entire programme. Well that is the intention though I am not sure how an implementer can guarantee it. Yes we can make it very unlikely that a generated unique identifier is not actually unique but it is not proof against Machiavelli. (generate an object file, clone it by another name and then link both files into the same executable.) > > ie even though uses the term "unnamed" the standard actually says it > has a name, it's just that you don't know what it is so can't use it. Yes, I first came across the concept in Forth. There has to be a name but it is unutterable (by the programmer). Actually Forth had a really unutterable name that it used for vocabulary roots, ' ' (i.e. a space) which was impossible for the programmer to utter from the keyboard (whitespace has no significance outside the inner workings of the implementation). Unfortunately C++ cannot be quite so clever because the names have to be readable by the linker. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Deleting a singleton object Next: How can I alter how a user-defined type/enum is displayed? (facet/locale?) |