9

I found a confusing thing about the "using" keyword. If I do using a class or struct, then it won't be necessary to do using functions in the same namespace which take that class or struct as an argument. Like the codes below.

namespace A
{
    struct testData
    {
        int x;
    };

    int testFunc(testData data)
    {
        return data.x;
    }
}

#include <cstdio>;

using A::testData;

int main()
{
    testData test = { 1 };
    printf("%d", testFunc(test));

    return 0;
}

I thought I should not be allowed to use testFunc() because I only use the "using" keyword for testData. However, these codes work just fine.

Could you please tell me why this works this way?

Astray Bi
  • 93
  • 3

1 Answers1

9

You are correct in how using works.

But you're forgetting one thing: argument-dependent lookup. The compiler can see testFunc via the test parameter supplied.

See http://en.cppreference.com/w/cpp/language/adl

Bathsheba
  • 231,907
  • 34
  • 361
  • 483