I would not have expected this code to compile, but it does. My understanding is that func(d)
it looks in the global namespace for a function called "func" but also in the namespace of any passed in parameters (Argument dependent lookup)
But in this case the parameter is in the global namespace. So why does it find "func" in the ns namespace? Are there special rules saying that if the parameter type is a typedef then it uses the namespace of the underlying type rather than the namespace of the actual parameter?
This appears to be true but I can't find anything supporting this... Is it the expected behavour?
namespace ns
{
struct data {};
void func(ns::data item) {}
};
// Create an alias "datatype" in the global namespace for ns::data
typedef ns::data datatype;
int main()
{
datatype d;
func(d);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…