Can someone explain the behavior I am seeing for namespace search for qualified names in the following example code? See inline comments for the issue.
namespace ns1::hw
{
struct A1
{
enum class E1
{
E1_1,
E1_2,
};
};
};
namespace ns1::ns2::hw
{
struct A2
{
enum class E1
{
E1_1,
E1_2,
};
};
};
namespace ns1::ns2::ns3::hw
{
struct A3
{
enum class E1
{
E1_1,
E1_2,
};
};
};
namespace ns1::ns2::ns3::ns4
{
struct A4
{
int I1 { (int)hw::A3::E1::E1_1 }; // <--- this compiles OK
// seems to search upwards in the parent namespace,
// looking for the relative partially qualified
// name.
int I2 { (int)hw::A2::E1::E1_1 }; // <--- this doesn't
// doesn't seem to apply the same search algorithm
// beyond the immediate parent namespace.
};
};
int main()
{
return 0;
}
In your first snippet:
as you seem to expect,
hwis looked for inns4. It doesn't exist, so it is looked for in the parent namespacens3. It findshwthere, and then it finds the nested namesA3, thenE1, and thenE1_1, and so the name lookup succeeds.In the second snippet:
actually the same rules are applied.
hwis looked for inns4. It doesn't exist, so it's looked for inns3. It's found there, but then it fails to find the nested nameA2.Now name lookup will only go upwards in a namespace hierarchy. It will go up as many levels as needed to find a name, but if it ever finds a match inside a namespace level, this process stops. If it then later finds a missing nested name, it won't go in reverse and continue looking upwards in the namespace hierarchy, it's just a hard error instead.
In this case, if there was no
hwinns3, then the lookup would have gone up to the parent namespacens2, and then the nested namesA2,E1, andE1_1would have been found successfully.