9

I have inherited a horrible bit of legacy code which includes about 1000 lines of utility class definition that needs to appear before the "real" code in a source file. To avoid clashes with other modules that might also have associated legacy classes, I have put the utility class into an unnamed namespace:

namespace {
    class OldUtils {
        OldUtils();
        int foo();
        double bar();
    };

    OldUtils::OldUtils() {
        // hundreds of lines
    }

    int OldUtils::foo() {
        // hundreds more lines
    }

    ...
}

class ActuallyInteresting {
    // uses OldUtils
};

But I would prefer to have the ActuallyInteresting code that people will be (actually) interested in near the top of the file, e.g. starting on line 50, than right at the bottom e.g. starting on line 1000. Splitting the horrid utility class into a separate compilation unit isn't an option, for higher-level reasons I won't go into!

So I am wondering if it is possible to put the short class declaration -- without method definitions -- in an unnamed namespace at the top of the file, and the much longer method definitions in another unnamed namespace at the bottom:

namespace {
    class OldUtils {
        OldUtils();
        int foo();
        double bar();
    };
}

class ActuallyInteresting {
    // uses OldUtils
};

namespace {
    OldUtils::OldUtils() {
        // hundreds of lines
    }

    int OldUtils::foo() {
        // hundreds more lines
    }

    ...
}

Will these two "separate" unnamed namespaces be treated as the same scope within the compilation unit, or will different unique namespaces be generated for each? Does the standard have anything to say about this?

andybuckley
  • 1,114
  • 2
  • 11
  • 24
  • Each unnamed namespace will be unique to each translation unit (same as static in a translation unit) –  Apr 27 '16 at 12:51
  • @DieterLücking: If this is true, how does one access a global variable in an unnamed namespace in another translation unit? Does the name of the other translation unit get added to the scope resolution text? – Thomas Matthews Apr 27 '16 at 15:28

1 Answers1

7

Will these two "separate" unnamed namespaces be treated as the same scope within the compilation unit, or will different unique namespaces be generated for each? Does the standard have anything to say about this?

Yes, they will be treated as same within the compilation unit. And the standard does have something to say....

Quoting the latest standard draft... (emphasis are mine)

$7.3.1.1 An unnamed-namespace-definition behaves as if it were replaced by

inlineopt namespace unique { /* empty body */ }
using namespace unique ;
namespace unique { namespace-body }

where inline appears if and only if it appears in the unnamed-namespace-definition and all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the translation unit

Compile this short program, your compiler should complain of variable redeclaration...

namespace { int c = 0; }
namespace { double c = 8; }

int main(){ ++c; }

However, to access variables in an unnamed namespace, you use the regular way of accessing global variables... Hence this will work.

namespace { int c = 0; }
namespace { void f(){ c = 8; } }

int main(){ ++c; }
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68