www.digitalmars.com         C & C++   DMDScript  

c++.beta - Name space lookup error

reply Steve <Steve_member pathlink.com> writes:
The following code cause the error

function 'foo' has no prototype

//==============================================
namespace foo_ns
{
void foo() {}
}

using namespace foo_ns;

int main()
{
::foo();

return 0;
}
Feb 01 2004
parent reply "Anton Sekeris" <no.spam inter.nl.net> writes:
I think the scope operator before the call of foo() is suggesting to
the compiler that foo can be found in the global namespace. Your using
directive is all that is required and foo can just be called as foo();

Anton

Steve wrote:

 The following code cause the error
 
 function 'foo' has no prototype
 
 //==============================================
 namespace foo_ns
 {
 void foo() {}
 }
 
 using namespace foo_ns;
 
 int main()
 {
 ::foo();
 
 return 0;
 }
Feb 01 2004
parent reply steve <steve_member pathlink.com> writes:
You are correct.  I posted this because I encountered the code that use the ::
to resolve the scope.  In fact, I worked around the code with foo_ns::foo.
However, I still think that the code in question is still legimate (even though
it was not a good practice). because it was compiled fine under MSVC, Borland,
GCC, Comeau, and Intel compilers but failed under DMC.

Steve

In article <bvjj20$i0g$1 digitaldaemon.com>, Anton Sekeris says...
I think the scope operator before the call of foo() is suggesting to
the compiler that foo can be found in the global namespace. Your using
directive is all that is required and foo can just be called as foo();

Anton

Steve wrote:

 The following code cause the error
 
 function 'foo' has no prototype
 
 //==============================================
 namespace foo_ns
 {
 void foo() {}
 }
 
 using namespace foo_ns;
 
 int main()
 {
 ::foo();
 
 return 0;
 }
Feb 01 2004
parent reply Scott Michel <scottm mordred.cs.ucla.edu> writes:
I've come across a few things in my code that DMC complains about that are
technically more to the standard than the other compilers. They're pretty
easy to work around, if annoying.

I'd have to look at the ARM or the actual spec to figure out if the usage
is incorrect, but I doubt it. For example, I found this question about
name spaces on Google which seems to buttress your assertion:

    TITLE: anonymous namespaces

    PROBLEM: jazzbeau netcom.com (Steve Barnette)

    Given the following code:

       static int i;

       namespace {
	    int j;
	}
	int main() {
	    ::i = 0;
	    ::j = 0;	// error?
	    return 0;
	}

    Should ::j allow access to the integer defined in the anonymous namespace
    as does the classic static definition?


    RESPONSE: clamage Eng.Sun.COM (Steve Clamage), 27 Oct 95

    An unnamed namespace definition acts as if it were followed by a
    using-directive for that (unnameable) namespace. The names declared
    in that namespace are thus brought into the enclosing scope. In
    your example, 'j' is brought into global scope. (Reference: WP
    section 7.3.1.1, 7.3.4)

    The unary '::' restricts name lookup to those names declared at global
    scope, or visible in global scope due to a using-declaration. (WP 3.4.2)

    Your example of "::j" therefore is valid and refers to the "j" in the
    unnamed namespace.

FWIW...


-scooter

steve <steve_member pathlink.com> wrote:
 You are correct.  I posted this because I encountered the code that use the ::
 to resolve the scope.  In fact, I worked around the code with foo_ns::foo.
 However, I still think that the code in question is still legimate (even though
 it was not a good practice). because it was compiled fine under MSVC, Borland,
 GCC, Comeau, and Intel compilers but failed under DMC.
 
 Steve
 
 In article <bvjj20$i0g$1 digitaldaemon.com>, Anton Sekeris says...
I think the scope operator before the call of foo() is suggesting to
the compiler that foo can be found in the global namespace. Your using
directive is all that is required and foo can just be called as foo();

Anton

Steve wrote:

 The following code cause the error
 
 function 'foo' has no prototype
 
 //==============================================
 namespace foo_ns
 {
 void foo() {}
 }
 
 using namespace foo_ns;
 
 int main()
 {
 ::foo();
 
 return 0;
 }
-- Scott Michel | No research proposal ever survives UCLA Computer Science | contact with implementation. PhD Graduate Student | !! Futuaris nisi irrisus ridebis !!
Feb 01 2004
parent "Anton Sekeris" <no.spam inter.nl.net> writes:
I decided to leaf through the standard based on your example, and you
are quite right.

On page 34, par. 3.4.3 (Qualified name lookup) sub 4 it says:

A name prefixed by the unary scope operator :: (5.1) is looked up in
global scope, in the translation unit
where it is used. The name shall be declared in global namespace scope
or shall be a name whose declaration
is visible in global scope because of a using directive
(3.4.3.2). The use of :: allows a global name to
be referred to even if its identifier has been hidden (3.3.7).

So the original code should indeed have compiled correctly.

Anton.
Feb 02 2004