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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With