Victor Bazarov
9/26/2008 4:00:00 PM
Ali Karaali wrote:
>> I think you should ask Comeau folks for the explanation. They have a
>> link on their site to send a question to the compiler team, use it.
>>
>> As I could understand from 3.3.1/6 (it's a note, hence non-normative,
>> but it explains the other elements), the name 'f' should be considered a
>> member of 'n', although it doesn't *introduce* that name (and that's why
>> you need the extra declaration to be able to use 'f' later). So the
>> compiler *should* connect the 'f' from the 'friend' declaration with the
>> 'f' from the namespace declaration and *merge* them because, according
>> to the Standard (at least how I understand it) they declare the *same*
>> function.
>
> void fonk (); // <-- Why do I have to declare it in here?
>
> /* If I don't declare it,
> #error `void fonk()' should have been declared inside `::'
Which line does it refer to? To the 'friend' declaration?
> */
>
> namespace MySpace
> {
> int a = 1, b = 2, c = 3;
>
> class Sample
> {
> int m_s;
>
> public :
> Sample()
> :
> m_s( 0 )
> {};
>
> int getSample()
> {
> return this->m_s;
> }
>
> friend void ::fonk();
I believe the reason the compiler needs 'fonk' declared in the global
namespace before you declare it here is due to your use of scope
resolution. If you didn't use it, the name would be looked up in the
namespace surrounding your class, and if the function isn't found, it
would be considered "forward declared" for the purposes of that class
definition only. But since you specified where to look for the function
by using '::', the compiler has to look and it doesn't find it.
> };
>
> int foo (Sample sample)
> {
> return sample.getSample();
> }
>
> int func (int arg)
> {
> return arg * arg;
> }
> }
>
> void fonk ()
> {
> MySpace::Sample sample;
>
> cout << sample.m_s << " " << 5 << endl;
> cout << "I am a friend";
> }
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask