W
wschlanger
Hi, the following code behaves differently for GCC/MSC and Borland C++
5.6.
The question was, what order does symbol lookup happen in?
1. declared symbols, including using'd symbols.
2. parent namespace (nested)
3. using'd namespaces.
The above order is used by GCC and MSC, but Borland C++ 5.6 has (2)
and (3) flipped so that the following code is capable of detecting
your compiler:
What does ANSI C++ say about this?
Since it depends on compilers, shouldn't gcc be generating a warning?
Or does GCC have it right and is Borland C++ what's wrong?
If you could please run this program on an Intel C++ compiler or other
compilers, I am interested in the outcome. What does it say when you
run it on your compiler (besides GCC, MSC, and BCC?)
Thanks in advance,
Willow
---
#include <stdio.h>
namespace NSX
{
int a;
}
namespace NS1
{
int a;
namespace NS2
{
using namespace NSX;
// parent (nested) NS2 'and' using'd namespace NSX have a.
int &b = a;
}
}
int main()
{
if(&NS1::NS2::b == &NSX::a)
printf("BCC detected.\n");
else
if(&NS1::NS2::b == &NS1::a)
printf("GCC/MSC detected.\n");
else
printf("Error.\n");
return 0;
}
5.6.
The question was, what order does symbol lookup happen in?
1. declared symbols, including using'd symbols.
2. parent namespace (nested)
3. using'd namespaces.
The above order is used by GCC and MSC, but Borland C++ 5.6 has (2)
and (3) flipped so that the following code is capable of detecting
your compiler:
What does ANSI C++ say about this?
Since it depends on compilers, shouldn't gcc be generating a warning?
Or does GCC have it right and is Borland C++ what's wrong?
If you could please run this program on an Intel C++ compiler or other
compilers, I am interested in the outcome. What does it say when you
run it on your compiler (besides GCC, MSC, and BCC?)
Thanks in advance,
Willow
---
#include <stdio.h>
namespace NSX
{
int a;
}
namespace NS1
{
int a;
namespace NS2
{
using namespace NSX;
// parent (nested) NS2 'and' using'd namespace NSX have a.
int &b = a;
}
}
int main()
{
if(&NS1::NS2::b == &NSX::a)
printf("BCC detected.\n");
else
if(&NS1::NS2::b == &NS1::a)
printf("GCC/MSC detected.\n");
else
printf("Error.\n");
return 0;
}