Question

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?

Was it helpful?

Solution

  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];
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top