Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

friend definition in nested class has private access to enclosing class #31095

Open
llvmbot opened this issue Jan 25, 2017 · 2 comments
Open
Labels
bugzilla Issues migrated from bugzilla c++ clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 25, 2017

Bugzilla Link 31747
Version trunk
OS All
Reporter LLVM Bugzilla Contributor
CC @DougGregor

Extended Description

Clang accepts ill-formed code in -std=c++11/14/1z:

struct Outer
{
    struct Inner
    {
        friend int get(Outer& o)
        {
            return o.x;
        }
    };
private:
    int x;
};

Legal basis (based on c++11 standard):
7.3.1.2[3], 11.3[6]: in-class friend function definition
is a namespace member
3.4.1[9], 11.3[7]: in-class friend function definition
has member function lexical scope
11.7[1]: in-nested-class function definition
can access private members of enclosing class
9.7[4]: in-nested-class friend function definition
has no special access rights to members of an enclosing class

It seems that explicit exception in 9.7[4] is not respected: "(...) friend function (...) has no special access rights to members of an enclosing class".
There's no warning with -Wall -Wextra -pedantic on clang-3.5, newer and trunk.

I'd expect compilation to fail with error. Other compilers (gcc, msvc, icc) fail as expected, noting that in given context x is private.

In c++98 code is ill-formed as well and clang incorrectly compiles it silently. In c++98 access from nested to enclosing class privates is illegal (11.8[1] c++03). In c++11 above clause was relaxed (11.7[1] c++11), but this shouldn't change the meaning of -std=c++98/03 in clang.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@shafik
Copy link
Collaborator

shafik commented Jun 21, 2023

This is DR 1699

Which is also covers: #44053

Although not identical the resolution to DR 1699 should resolve all of these.

Also see cplusplus/papers#1573

@EugeneZelenko EugeneZelenko added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Jun 21, 2023
@llvmbot
Copy link
Collaborator Author

llvmbot commented Jun 21, 2023

@llvm/issue-subscribers-clang-frontend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++ clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

3 participants