How does the using directive affect function arguments in C ++?

I have the following code that works fine using g ++ 4.4.6, but won't compile using Visual Studio 2008. It seems to be related to search argument dependent, so I think g ++ is correct.

// testClass.hpp
namespace test {
    class foo {
      public:
        foo(){}
     };

    class usesFoo {
      public:
        usesFoo() {}

        void memberFunc(foo &input);
    };
}

// testClass.cpp
#include "testClass.hpp"

using test::usesFoo;

void usesFoo::memberFunc(foo &input) {
    (void) input;
}

      

Compile error in Visual Studio,

1> Compilation ...
1> testClass.cpp 1> c: \ work \ testproject \ testproject \ testclass.cpp (6): error C2065: 'foo': uneclared identifier 1> c: \ work \ testproject \ testproject \ testclass .cpp (6): error C2065: 'input': undeclared identifier 1> c: \ work \ testproject \ testproject \ testclass.cpp (6): error C2448: 'test :: usesFoo :: memberFunc': functional style initializer, appears to be a function definition

I realize that either putting the namespace directly into a member function in the cpp file or "using a namespace test" will fix the problem, I'm more curious what exactly this standard says in this case.

+3


source to share


1 answer


The code is correct, but it has nothing to do with search argument dependent. In addition, the use of the declaration only affects the detection usesFoo

, not foo

: after you speak the name of a class member, other names are looked up in the context of that class. Since it foo

is a member of the :: usesFoo` test, it found. Without using a directive, you need to define the member function like this:

void test::usesFoo::memberFunction(foo& input) {
    (void)input;
}

      

The corresponding proposal for this is 3.4.1. Unqualified name Look-up [basic.lookup.unqual] item 6:



A name used in a function definition following a declarator-id function that is a member of the N namespace (where, for purposes of this presentation only, N can represent the global scope) must be declared before it is used in the block in which it is used, or in one of the its closing blocks (6.3), either must be declared before it is used in the N namespace or, if N is a nested namespace, must be declared before it is used in one of the Ns enclosing namespaces.

Argument-dependent search only goes into the image when the function is called, not when it is defined. These things have nothing to do with each other.

+1


source







All Articles