Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does "using" keyword do in c++? [duplicate]

Tags:

c++

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?

like image 453
Astray Bi Avatar asked Jun 23 '16 14:06

Astray Bi


1 Answers

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

like image 142
Bathsheba Avatar answered Nov 15 '22 16:11

Bathsheba