Bug 48884 - decltype's operand doesn't consider friend declaration
Summary: decltype's operand doesn't consider friend declaration
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 4.6.1
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2011-05-05 11:50 UTC by Ryou Ezoe
Modified: 2011-05-25 01:18 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-05-05 12:35:32


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ryou Ezoe 2011-05-05 11:50:48 UTC
decltype's operand doesn't consider friend declaration if it's used as a return type of the function.
Following code failed.

class X
{
    int value ;
    friend class Y ;
} ;

class Y
{
public :
    template < typename T >
    static auto f(T t) -> decltype( t.value )
    {
        return t.value ; // OK. Y is a friend of X.
    }
} ;
 
int main()
{
    X x ;
    Y::f(x) ; // gcc issues error
}

The error message is 

In function 'int main()':
error: 'int X::value' is private
 error: within this context

It looks like access checking is done in the main rather than Y::f<X>.
Comment 1 Jonathan Wakely 2011-05-05 12:35:32 UTC
confirmed

if Y::f is not a template then access checking succeeds

here's a C++03 version, which fails in the same way:

class X
{
    int value ;
    friend class Y ;
} ;

template <int> struct Int { };

class Y
{
public :
    template < typename T >
    static Int<sizeof(T::value)> f(T t)
    {
        return Int<sizeof(t.value)>();
    }
} ;

int main()
{
    X x ;
    Y::f(x) ; // gcc issues error
}
Comment 2 Jason Merrill 2011-05-24 20:26:51 UTC
Author: jason
Date: Tue May 24 20:26:47 2011
New Revision: 174139

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=174139
Log:
	PR c++/48884
	* class.c (pushclass): Accept NULL argument.
	(popclass): Deal with popping null class.
	* pt.c (push_access_scope, pop_access_scope): Use them rather than
	push_to_top_level/pop_from_top_level.
	(push_deduction_access_scope, pop_defarg_context): New.
	(fn_type_unification): Use them.
	* name-lookup.c (lookup_name_real_1): Check current_class_type.

Added:
    trunk/gcc/testsuite/g++.dg/template/access21.C
    trunk/gcc/testsuite/g++.dg/template/access22.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/class.c
    trunk/gcc/cp/name-lookup.c
    trunk/gcc/cp/pt.c
    trunk/gcc/testsuite/ChangeLog
Comment 3 Jason Merrill 2011-05-25 01:08:48 UTC
Author: jason
Date: Wed May 25 01:08:46 2011
New Revision: 174163

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=174163
Log:
	PR c++/48884
	* pt.c (fn_type_unification): Disable access control during
	substitution.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/template/access21.C
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/template/access22.C
Modified:
    branches/gcc-4_6-branch/gcc/cp/ChangeLog
    branches/gcc-4_6-branch/gcc/cp/pt.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 4 Jason Merrill 2011-05-25 01:18:52 UTC
Fixed for 4.6.1.