[cfe-dev] Friend functions default parameters behavior

Александр Овчинников sanek23994 at gmail.com
Wed Jul 23 04:48:49 PDT 2014


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140723/83a1bfcb/attachment.html>


More information about the cfe-dev mailing list