Why does "#define A" interfere with "namespace A{}"?

BugRepairMan

The following code can compile:

namespace A{
    int i;
}
namespace B{
    int i;
}
int main(){ return 0; }

But the following code cannot compile:

#define A
#define B

namespace A{
    int i;
}
namespace B{
    int i;
}
int main(){ return 0; }

The error info is

error: redefinition of 'int {anonymous}::i'

After I define A and B why do the names of namespaces become anonymous?

The used compiler: gcc-4.9.3.

NathanOliver

In

#define A
#define B

namespace A{
    int i;
}
namespace B{
    int i;
}

You define A and B to be nothing. That means your code becomes

namespace {
    int i;
}
namespace {
    int i;
}

After the preprocessor runs. Since both namespaces become anonymous namespaces the compiler correctly complains that you are redeclaring i.

Remember that when you define something the preprocessor is going to do through your source code and replace all occurrences of that symbol with whatever you defined it to be. Had you done

#define A LONG_NAME_I_DO_NOT_WANT_TO_TYPE
#define B ANOTHER_LONG_NAME_THAT_I_ALSO_DO_NOT_WANT_TO_TYPE

namespace A{
    int i;
}
namespace B{
    int i;
}

Then the preprocessor would change the code to be

namespace LONG_NAME_I_DO_NOT_WANT_TO_TYPE{
    int i;
}
namespace ANOTHER_LONG_NAME_THAT_I_ALSO_DO_NOT_WANT_TO_TYPE{
    int i;
}

For more information on how the preprocessor works see: GCC - The C Preprocessor

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does Iterator define the remove() operation?

Why does some libs define their own collections?

why does a local PrintWriter interfere with another local PrintWriter?

Does Git interfere with SVN?

Why does "position: relative" interfere with "transform: scale"?

Why does the Linux kernel #define a symbol as itself?

Why does a nested self-capturing function interfere with isKnownUniquelyReferenced(_:)?

Why does enabling undefined behaviour sanitization interfere with optimizations?

Why OpenAPI does not define '$ref' as allowed property?

Why does "using namespace" try to instantiate templates in that namespace for MSVC

Why does my Navbar interfere with my Stripe token creation?

Why does namespace usage break MinGW compilation?

Why does anonymous namespace give redefinition error?

How does named element inside form interfere with global namespace?

Why does the glTF schema define enums like this?

Why does socket interfere with selenium?

Why does React fail to define 'App' component?

Why does DateTime() not define midnight?

Why does optimizing with the -O(1/2/3) option interfere with scanf()?

Implementing a Web Api Controller - why does use of IDisposable interfere with routing?

Why does my firewall (iptables) interfere in my bridge (brctl)?

Why does XmlTypeAttribute.Namespace not set a namespace for the root element?

Maven deploy-file goal: Why does the first execution interfere with the second one?

Why does anyone use define(['jquery'...], function($...){...}

Why does the NEG instruction interfere with the Carry Flag?

Does requestAnimationFrame interfere CSS transitions?

Why does DispatchGroup interfere with main queue?

why does define conflict with enum class?

Why does remapping key interfere with FormatTime?