The namespace of local function declarations in C++ - by Uray M. János

Status : 

  Deferred<br /><br />
		The product team has reviewed this issue and has deferred it for consideration at a later time.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
0
Sign in
to vote
ID 664619 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 4/25/2011 4:32:43 AM
Access Restriction Public

Description

The following C++ code:

namespace NS {
	struct S {
		void f ();
	};
}
int main () {
	NS::S s;
	s.f();
}
namespace NS {
	void S::f () {
		extern void g ();
		g();
	}
	void g () {
	}
}

fails at link-time when compiled with Microsoft Visual C++ 2010: error LNK2019: unresolved external symbol "void __cdecl g(void)" (?g@@YAXXZ) referenced in function "public: void __thiscall NS::S::f(void)" (?f@S@NS@@QAEXXZ)

If I remove the namespace NS, it works. If f is not a member-function, it works. But in this code, the compiler thinks that the "extern void g ();" is in the global namespace, and not in namespace NS. However, the Standard 3.5/7. says: "When a block scope declaration of an entity with linkage is not found to refer to some other declaration, then that entity is a member of the innermost enclosing namespace." So I think, it is a bug.
Sign in to post a comment.
Posted by Microsoft on 5/10/2011 at 6:51 PM
Hello,

Thank you for reporting this bug. This looks like indeed a compiler where we're not injecting the extern decl to the inner-most namespace scope, instead we're injecting it to the global scope. We will consider fixing this bug in a future release. In the meantime, please accept these workarounds, and we apologize for the inconvienence.

namespace NS {
    struct S {
        void f ();
    };
}
int main () {
    NS::S s;
    s.f();
}
namespace NS {


    void S::f () {
        extern void g ();
        g();
    }
}

void g () {
}

Thanks,
Ulzii Luvsanbat
Windows C++ Team
Posted by Microsoft on 4/25/2011 at 8:20 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Posted by Microsoft on 4/25/2011 at 5:12 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)