0

A local class in C++ can have friend functions, but these functions cannot be defined neither inside the class [class.friend] p5:

A function may be defined in a friend declaration of a class if and only if the class is a non-local class and the function name is unqualified.

nor inside the enclosing function [dcl.fct.def.general] p2:

[...] A function shall be defined only in namespace or class scope. [...]

And what if a friend function of a local class is defined in the global scope as follows:

auto foo() {
    struct A;
    void bar(const A&);
    struct A { 
        friend void bar(const A&);
    };
    bar(A{});
    return A{};
}

using A = decltype(foo());
void bar(const A&) {}

int main() {
    foo();
}

Compilers diverge here:

  • MSVC shows a compilation warning and fails during linking:
warning C5046: 'bar': Symbol involving type with internal linkage not defined
error LNK2019: unresolved external symbol "void __cdecl bar(struct `__cdecl foo(void)'::`2'::A const &)" (?bar@@YAXAEBUA@?1??foo@@YA@XZ@@Z) referenced in function "__cdecl foo(void)" (?foo@@YA@XZ)
  • EDG shows compilation error:
error: function "bar", declared using a local type, must be defined in this translation unit
  • GCC already accepts the program, but only with -fpermissive flag, printing the warning:
warning: 'void bar(const foo()::A&)', declared using local type 'const foo()::A', is used but never defined [-fpermissive]
  • and only Clang permits this program just fine without single warning: online demo.

Which implementation is correct here?

8
  • Use the scoping operator :: to explicitly use the global namespace scope? As in friend void ::bar(const A&); Commented Aug 22, 2023 at 19:29
  • 1
    Such a code lacks of readability. What a practical usage does come to such a code? Commented Aug 22, 2023 at 19:31
  • @Someprogrammerdude qualified and unqualified friend function names are covered by different rules Commented Aug 22, 2023 at 19:34
  • 1
    I think eel.is/c++draft/dcl.meaning.general#2.3 suggests the code is ok Commented Aug 22, 2023 at 19:36
  • 1
    Then again, there's stackoverflow.com/a/71168429/817643 - But i'm not sure local classes were in mind here Commented Sep 26, 2023 at 16:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.