0

In a .cpp file, an anonymous namespace basically has file-wide linkage (after #includes) because the .cpp file will never be included by another file. But, the same pattern in a header file propagates that anonymous namespace to wherever it is included in. Is there a way to create a similar effect in a header file? I ask because template implementations must be in headers.

A simple example in a regular .h file would be this:

// object.h

namespace {
    using verbose::namespace::type;
}

...

struct object {  
    type value;
}

or similarly in some template implementation file. The type type would be in-scope of wherever this file is included.

Is there a way around this?

EDIT: I think I found a verbose but workable answer.

// object.h

struct Namespace {
     using verbose::namespace::type;

     Namespace() = delete;

     struct object {
         type value;
     };
};

using Namespace::object;
Anthony
  • 1,015
  • 8
  • 22
  • Just add `using type = verbose::namespace::type;` to the class? – NathanOliver Aug 02 '18 at 21:07
  • I strongly prefer to not mix implementation style with the implementation itself. I could, but then object::type would be valid and that just seems messy. Even if I made it protected, then subclasses could see it. Making it private would disallow me to add a public using `type` to object in the future. – Anthony Aug 02 '18 at 21:09
  • Yeah, that would work, and I wouldn't lose any ability. I'm trying to think of side-effects but I can't think of any (the point I edited into my comment above is invalid, cause the name would be taken anyways with the anon namespace). – Anthony Aug 02 '18 at 21:13
  • It would be nice to separate it from the struct's definition though. – Anthony Aug 02 '18 at 21:14
  • Since you don't want to pollute the global scope of anything that includes your file with that name you have to scope it somehow. One option is to put all the code in the header in it's own namespace. Another option is to scope the name to the class that is using it. – NathanOliver Aug 02 '18 at 21:16
  • You can even do something like `namespace detail { using verbose::namespace::type; }` and then later use `detail::type`. – NathanOliver Aug 02 '18 at 21:17
  • @NathanOliver What do you think of the added edit? – Anthony Aug 02 '18 at 21:18
  • Is `using Namespace::object;` really needed? Any source file could have that is they don't want to type `Namespace::object` all the time. – NathanOliver Aug 02 '18 at 21:19
  • Wouldn't `using Namespace::object;` make it so they _don't_ have to do that? I could be misunderstanding. – Anthony Aug 02 '18 at 21:20
  • 1
    Yes, it would mean they don't have to do that but it also means that anyone that includes your header can't use the name `object` in their own code. If you don't use `using Namespace::object;` then you give the option of having that instead of forcing it on them. – NathanOliver Aug 02 '18 at 21:22
  • True. In my head I knew there'd be a real namespace around this code (as I always do in projects); I should have added the surrounding namespace in the example. I agree with you. In this case I'd prefer a namespace instead of the `struct Namespace` because it's extendable in other files. – Anthony Aug 02 '18 at 21:26
  • 1
    Oops. I just realized you had `struct Namespace`. My eyes lied to me and I though I saw `namespace Namespace`. – NathanOliver Aug 02 '18 at 21:27
  • @NathanOliver no worries! – Anthony Aug 06 '18 at 14:26

1 Answers1

-2

This should do the trick:

// object.h
{
    namespace {
        using verbose::namespace::type;
    }

    ...

    struct object {  
        type value;
    }
}

Namespaces should only be valid within the code block they're defined in.

Nicholas Pipitone
  • 4,002
  • 4
  • 24
  • 39