0

I perform a refactoring of my project and I want to get rid of all anonymous namespaces, including classes, structs, unions. I want to replace them at the easiest way by their named equivalents. As far as I know, the equivalent for anonymous namespace is:

Such code:

namespace { namespace-body }

is equivalent to:

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

link: Anonymous Namespace Ambiguity

In such easy cases It is pretty enough to set just the unique name for struct, union, class:

1) Such code:

typedef struct { int a; void func() {}; } s1;

is equivalent to

typedef struct unique { int a; void func() {}; } s1;

2) Such code:

    struct vec3 { 
            struct {
                    class
                    {
                        public:
                        void foo();
                    } bar;
                float x, y, z;
            } hotel; 
            float xyz[3];
    };

int main() {
    vec3 v;
    return 0;
}

is equivalent to:

struct vec3 { 
        struct wigwam {
                class skyscraper
                {
                    public:
                    void foo();
                } bar;
            float x, y, z;
        } hotel; 
        float xyz[3];
};

int main() {
    vec3 v;
    return 0;
}

But what should I do in such case, I have no idea:

//Problem example
struct vec3 { 

        struct {
                class
                {
                    public:
                    void foo();
                } bar;  
            float x, y, z;
        } ; 
        float xyz[3];
};

int main() {
    vec3 v;
    v.x = 10; //direct access to the internal variable of anonymous struct.
    return 0;
}

As we can see, it is possible to access the member of the anonymous struct in the same way as this anonymous struct is an anonymous namespace. But it isn't possible to define struct in a similar way. For example:

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

It isn't also possible to define a namespace within struct, so it isn't possible to just replace "struct" on "namespace" keyword for this example.

So, what is the easiest way to set the name for anonymous struct, union, class for a "Problem example" and generally for all possible examples?

Community
  • 1
  • 1
Lucky Man
  • 1,488
  • 3
  • 19
  • 41
  • 1
    Why is this a goal? Anonymous namespaces have a purpose; so do unnamed structs, classes, and unions. Having to make up non-conflicting names for things that don't need them won't help eliminate name conflicts for things that do need them. – Pete Becker Feb 28 '13 at 13:34
  • I really don't get what you are trying to achieve, if you want to improve the code stop making anonymous structs, namespace, 1 off instances, typedef struct notation and so on. – 111111 Feb 28 '13 at 13:36
  • give the anonymous namespace a unique name, then merge (the uniquely-named namespaces) where there are no conflicts, if desired. – justin Feb 28 '13 at 13:38
  • Pete Becker, 111111, of course my question looks rediculous. But my project is ok without anonymous namespaces. They are just unnecessary. – Lucky Man Feb 28 '13 at 13:51
  • justin, could you explain it more detailed. – Lucky Man Feb 28 '13 at 13:52

1 Answers1

2
  1. No, a named namespace is not the same as an anonymous namespace. The primary use of anonymous namespaces is to have all their content have internal linkage - meaning that other compilation units can not refer to it (e.g. call functions). Therefore don't rename anonymous namespaces. It's like removing the static specifier from a static function.
  2. typedef struct { int a; void func() {}; } s1; can be better replaced by struct s1 { int a; void func() {} }; - no additional "unique" identifier needed. Same applies for union. The whole typedef struct { /*def*/ } Blah; construct is in fact a C remnant and not necessary in C++.

To your actual problem example: The inner struct has not much effect except perhaps introducing padding bytes. So this should be equivalent and do the job:

//Problem example, revisited
struct vec3 { 

  class Bar {
  public:
    void foo();
  };

  Bar bar;
  float x, y, z;
  float xyz[3];
};
Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
  • Thnaks a lot! It's just what I need! :) – Lucky Man Feb 28 '13 at 14:33
  • Just remeber to not blindly change code that needs not be changed. If it works as it is, leave it be. If you have to work with it (i.e. change it), then put it under test and refactor it. But do so with thought, don't just throw a name at anything unnamed that comes across your path. – Arne Mertz Feb 28 '13 at 14:37