[cfe-dev] Friend functions default parameters behavior

Александр Овчинников sanek23994 at gmail.com
Thu Jul 24 14:47:20 PDT 2014


Hello,
Thanks for the answer.

Can you clarify the clang description:
As I understand we should to move definition of the function to the header
file,  which contains declaration of the class and the friend function. Am
I right? If so, I just think that this doesn't better way.



2014-07-24 5:44 GMT+04:00 Nikola Smiljanic <popizdeh at gmail.com>:

> This is taken from clang source at the point where the error is emitted:
>
> // C++11 [dcl.fct.default]p4: If a friend declaration specifies a
> // default argument expression, that declaration shall be a definition
> // and shall be the only declaration of the function or function
> // template in the translation unit.
>
> Not sure how wide spread this construct is, I'm seeing it for the first
> time. The code is not hard to fix and error seems appropriate to me.
>
>
> On Wed, Jul 23, 2014 at 9:48 PM, Александр Овчинников <
> sanek23994 at gmail.com> wrote:
>
>> Hi all,
>> In the http://clang.debian.net/ project Sylvestre, Arthur and I are
>> trying to rebuild Debian with clang. And we have the problems with default
>> arguments in the friend methods. (
>> http://clang.debian.net/status.php?version=3.4.2&key=WRONG_DEFAULT_DECLARATION).
>> This issue affects 19 packages in the Debian rebuild.
>>
>> In the process of the rebuild Debian packages we had created simple
>> example which contains different behavior with gcc and clang. Please see
>> below:
>>
>> *Simple example *
>> int func1(int i, int j) { return i + j; }
>>
>> class foo {
>> friend int func1(int i, int j = 0);
>> public:
>>     int func();
>> };
>>
>> int foo::func() { return func1(1); }
>>
>> int main() {
>>     foo a;
>>     a.func();
>>     return 0;
>> }
>>
>> Clang compilation error:
>> test.cpp:8:12: error: friend declaration specifying a default argument
>> must be the only declaration
>> friend int func1(int i, int j = 0);
>>            ^
>> test.cpp:1:5: note: previous declaration is here
>> int func1(int i, int j)
>>     ^
>> test.cpp:15:12: error: no matching function for call to 'func1'
>>     return func1(1);
>>            ^~~~~
>> test.cpp:1:5: note: candidate function not viable: requires 2 arguments,
>> but 1 was provided
>>
>> int func1(int i, int j)
>>     ^
>> 2 errors generated.
>>
>> The provided example successfully complies by gcc. So as it seems for the
>> clang successfully compilation failed packages requires patches.
>> As I understand the following code is correct way to using default
>> arguments with friend functions:
>>
>> int func1(int i, int j = 0);
>>
>> class foo {
>> friend int func1(int i, int j);
>> public:
>>     int func();
>> };
>>
>> int foo::func() { return func1(1); }
>>
>> int main() {
>>     foo a;
>>     a.func();
>>     return 0;
>> }
>>
>> int func1(int i, int j) { return i + j; }
>>
>> Even if clang seems to follow the C++ specification, I wonder if clang is
>> not too strict here.
>> For example, this patch, to make sure that the code compiles with clang,
>> I had to basically get ride of the default feature:
>>
>> https://bugs.debian.org/cgi-bin/bugreport.cgi?msg=5;filename=gfan.diff;att=1;bug=755308
>> to be able to compile the code with clang without touching the API.
>>
>>
>> --
>> Alexander
>>
>> Best regards.
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>>
>


-- 
Alexander

Best regards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140725/7dbf1a68/attachment.html>


More information about the cfe-dev mailing list