diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 23447326db5901444c9e0d9a090a7b981c9b50b5..bba2e8e94f0e78d8f2a3d9ef18a8a989f20ac698 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2005-10-20  Mark Mitchell  <mark@codesourcery.com>
+
+	PR c++/22618
+	* search.c (accessible_p): Check access in the outermost set of
+	template parameters.
+
 2005-10-20  Richard Guenther  <rguenther@suse.de>
 
 	* decl.c (grokdeclarator): Fix ambiguous pedwarn message.
diff --git a/gcc/cp/search.c b/gcc/cp/search.c
index 795e8c2e44f0dc2fdb0e992d0c3133bf2071bc31..090510b4103b04ff1d25e59c61fc868aba33a267 100644
--- a/gcc/cp/search.c
+++ b/gcc/cp/search.c
@@ -872,8 +872,12 @@ accessible_p (tree type, tree decl, bool consider_local_p)
   /* In a template declaration, we cannot be sure whether the
      particular specialization that is instantiated will be a friend
      or not.  Therefore, all access checks are deferred until
-     instantiation.  */
-  if (processing_template_decl)
+     instantiation.  However, PROCESSING_TEMPLATE_DECL is set in the
+     parameter list for a template (because we may see dependent types
+     in default arguments for template parameters), and access
+     checking should be performed in the outermost parameter list.  */ 
+  if (processing_template_decl 
+      && (!processing_template_parmlist || processing_template_decl > 1))
     return 1;
 
   if (!TYPE_P (type))
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index b94bce86936e8e2a3f466961bb54af913135cd17..4a63875dc99561ed49310d9a083928b5ce827f4e 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2005-10-20  Mark Mitchell  <mark@codesourcery.com>
+
+	PR c++/22618
+	* g++.dg/parse/access8.C: New test.
+
 2005-10-20  Richard Guenther  <rguenther@suse.de>
 
 	PR c++/24439
diff --git a/gcc/testsuite/g++.dg/parse/access8.C b/gcc/testsuite/g++.dg/parse/access8.C
new file mode 100644
index 0000000000000000000000000000000000000000..badea1c98b73d32badd26352adeb74ce5c26537b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/access8.C
@@ -0,0 +1,12 @@
+// PR c++/22618
+
+class foo
+{
+  typedef int memfun;  // { dg-error "private" }
+};
+
+template<foo::memfun> // { dg-error "context" } 
+struct fm_obj { };
+
+template <typename T = foo::memfun> // { dg-error "context" }
+struct S {};