public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* different friend functions behaviour
@ 2009-04-07 15:37 Fabio Tesser
  2009-04-07 16:45 ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Fabio Tesser @ 2009-04-07 15:37 UTC (permalink / raw)
  To: gcc-help

Hello gcc-help list,

I notice a strange (as my opinion) behaviour on gcc version 4.3.2.

I know that started from gcc-4.1 "ARM-style name-injection of friend 
declarations is no longer the default" .

So the following code doesn't build:
$ cat test_friend_function_1.cpp

class test {
public:
friend void ftest1(int x) {
}

};

int main(int argc, char *argv[]) {
ftest1(5);
}

$ g++ test_friend_function_1.cpp
test_friend_function_1.cpp: In function ‘int main(int, char**)’:
test_friend_function_1.cpp:10: error: ‘ftest1’ was not declared in this 
scope
$
... BUILD ERRORS

So as it's shown in http://gcc.gnu.org/ml/gcc-bugs/2008-08/msg00860.html 
it is necessary to use -ffriend-injection option.

$ g++ -ffriend-injection test_friend_function_1.cpp
$
... BUILD SUCCESS

But if I try with the following code:
$ cat test_friend_function_2.cpp

class test {
public:
friend void ftest2(test x) {
}

};

int main(int argc, char *argv[]) {
ftest2(test());
}

$ g++ test_friend_function_2.cpp
$
... BUILD SUCCESS

The build process goes even without the -ffriend-injection option.

The difference between the two friend functions is that the first has 
not any test class as argument while the second has a test class as 
argument.

Is this the right behaviour?

Thank you in advance for answers and/or comments.


Fabio Tesser














^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: different friend functions behaviour
  2009-04-07 15:37 different friend functions behaviour Fabio Tesser
@ 2009-04-07 16:45 ` Ian Lance Taylor
  2009-04-08  9:38   ` Fabio Tesser
  0 siblings, 1 reply; 4+ messages in thread
From: Ian Lance Taylor @ 2009-04-07 16:45 UTC (permalink / raw)
  To: Fabio Tesser; +Cc: gcc-help

Fabio Tesser <fabio.tesser@gmail.com> writes:

> The difference between the two friend functions is that the first has
> not any test class as argument while the second has a test class as
> argument.
>
> Is this the right behaviour?

Yes, this is correct.  This is Argument Dependent Lookup, also known as
Koenig lookup.

http://en.wikipedia.org/wiki/Argument_dependent_lookup

Ian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: different friend functions behaviour
  2009-04-07 16:45 ` Ian Lance Taylor
@ 2009-04-08  9:38   ` Fabio Tesser
  2009-04-08 13:48     ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Fabio Tesser @ 2009-04-08  9:38 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

Thanks for reply Ian.

The Argument Dependent Lookup should be active even inside a template class?
If I try put the friend function inside a template, g++ (version 4.3.2) 
gives me the following error:

$ cat test_template_friend_function.cpp
////////////////////////////////////
class test {
public:
friend void ftest2(test x) {
}
};

template <class T>
class simple_template
{
public:
void ftest3(T){
ftest2(test());
}
};

int main(int argc, char *argv[]) {
simple_template<int> a;
a.ftest3(0);
}
/////////////////////////////////////////

$ g++ test_template_friend_function.cpp
test_template_friend_function.cpp: In member function ‘void 
simple_template<T>::ftest3(T) [with T = int]’:
test_template_friend_function.cpp:18: instantiated from here
test_template_friend_function.cpp:12: error: no matching function for 
call to ‘ftest2(test)’

The same code compiles without errors with gcc version 4.2.4.
In your opinion what is the correct behavior?

Thanks

Fabio




On 04/07/2009 06:44 PM, Ian Lance Taylor wrote:
> Fabio Tesser <fabio.tesser@gmail.com> writes:
>
>   
>> The difference between the two friend functions is that the first has
>> not any test class as argument while the second has a test class as
>> argument.
>>
>> Is this the right behaviour?
>>     
>
> Yes, this is correct.  This is Argument Dependent Lookup, also known as
> Koenig lookup.
>
> http://en.wikipedia.org/wiki/Argument_dependent_lookup
>
> Ian
>   

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: different friend functions behaviour
  2009-04-08  9:38   ` Fabio Tesser
@ 2009-04-08 13:48     ` Ian Lance Taylor
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2009-04-08 13:48 UTC (permalink / raw)
  To: Fabio Tesser; +Cc: gcc-help

Fabio Tesser <fabio.tesser@gmail.com> writes:

> The same code compiles without errors with gcc version 4.2.4.
> In your opinion what is the correct behavior?

I don't know the C++ standard well enough to know the correct answer.
If you don't get an answer here, try asking on a C++ language group.

Ian

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-04-08 13:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-07 15:37 different friend functions behaviour Fabio Tesser
2009-04-07 16:45 ` Ian Lance Taylor
2009-04-08  9:38   ` Fabio Tesser
2009-04-08 13:48     ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).