W
WaterWalk
I find friend declaration just very tricky. I tried the following
examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are
surprising.
Example1:
namespace ns1
{
class Test
{
friend void func()
{
printf("func in %s\n", __FILE__);
}
};
}
int main()
{
ns1::func();
}
---End Example1---
gcc: compiles and works
VC++: compile error: 'func': candidate function(s) not accessible
Example2:
-file1.cpp
namespace ns1
{
class Test
{
friend void func();
};
}
int main()
{
ns1::func2("abc");
}
-file2.cpp
namespace ns1
{
void func()
{
printf("func in %s\n", __FILE__);
}
}
---end Example2---
gcc: compiles and works
VC++: compiles and works
I'm rather confused. Does a friend function declaration make it as if
it's also declared in the enclosing namespace? At least in example2,
it seems to be so.
What about Example1? What is the right behavior?
examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are
surprising.
Example1:
namespace ns1
{
class Test
{
friend void func()
{
printf("func in %s\n", __FILE__);
}
};
}
int main()
{
ns1::func();
}
---End Example1---
gcc: compiles and works
VC++: compile error: 'func': candidate function(s) not accessible
Example2:
-file1.cpp
namespace ns1
{
class Test
{
friend void func();
};
}
int main()
{
ns1::func2("abc");
}
-file2.cpp
namespace ns1
{
void func()
{
printf("func in %s\n", __FILE__);
}
}
---end Example2---
gcc: compiles and works
VC++: compiles and works
I'm rather confused. Does a friend function declaration make it as if
it's also declared in the enclosing namespace? At least in example2,
it seems to be so.
What about Example1? What is the right behavior?