diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index da63d51d9bc..8a44218611f 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -5551,6 +5551,8 @@ enum tsubst_flags {
 				(build_target_expr and friends) */
   tf_norm = 1 << 11,		 /* Build diagnostic information during
 				    constraint normalization.  */
+  tf_tst_ok = 1 << 12,		 /* Allow a typename-specifier to name
+				    a template.  */
   /* Convenient substitution flags combinations.  */
   tf_warning_or_error = tf_warning | tf_error
 };
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 7f80f9d4d7a..23c06655bde 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -4148,10 +4148,16 @@ make_typename_type (tree context, tree name, enum tag_types tag_type,
     }
   if (!want_template && TREE_CODE (t) != TYPE_DECL)
     {
-      if (complain & tf_error)
-	error ("%<typename %T::%D%> names %q#T, which is not a type",
-	       context, name, t);
-      return error_mark_node;
+      if ((complain & tf_tst_ok) && DECL_TYPE_TEMPLATE_P (t))
+	/* The caller permits this typename-specifier to name a template
+	   (because it appears in a CTAD-enabled context).  */;
+      else
+	{
+	  if (complain & tf_error)
+	    error ("%<typename %T::%D%> names %q#T, which is not a type",
+		   context, name, t);
+	  return error_mark_node;
+	}
     }
 
   if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 8fb17349ee1..18a21572ce3 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -14926,7 +14926,10 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
 		&& VAR_HAD_UNKNOWN_BOUND (t)
 		&& type != error_mark_node)
 	      type = strip_array_domain (type);
-	    type = tsubst (type, args, complain, in_decl);
+	    tsubst_flags_t tcomplain = complain;
+	    if (VAR_P (t))
+	      tcomplain |= tf_tst_ok;
+	    type = tsubst (type, args, tcomplain, in_decl);
 	    /* Substituting the type might have recursively instantiated this
 	       same alias (c++/86171).  */
 	    if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
@@ -15612,6 +15615,9 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   bool fndecl_type = (complain & tf_fndecl_type);
   complain &= ~tf_fndecl_type;
 
+  bool tst_ok = (complain & tf_tst_ok);
+  complain &= ~tf_tst_ok;
+
   if (type
       && code != TYPENAME_TYPE
       && code != TEMPLATE_TYPE_PARM
@@ -16199,8 +16205,10 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	      return error_mark_node;
 	  }
 
-	f = make_typename_type (ctx, f, typename_type,
-				complain | tf_keep_type_decl);
+	tsubst_flags_t tcomplain = complain | tf_keep_type_decl;
+	if (tst_ok)
+	  tcomplain |= tf_tst_ok;
+	f = make_typename_type (ctx, f, typename_type, tcomplain);
 	if (f == error_mark_node)
 	  return f;
 	if (TREE_CODE (f) == TYPE_DECL)
@@ -16229,6 +16237,12 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	      }
 	  }
 
+	if (TREE_CODE (f) == TEMPLATE_DECL)
+	  {
+	    gcc_checking_assert (tst_ok);
+	    f = make_template_placeholder (f);
+	  }
+
 	return cp_build_qualified_type_real
 	  (f, cp_type_quals (f) | cp_type_quals (t), complain);
       }
@@ -17107,7 +17121,10 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
     case CONVERT_EXPR:
     case NOP_EXPR:
       {
-	tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
+	tsubst_flags_t tcomplain = complain;
+	if (code == CAST_EXPR)
+	  tcomplain |= tf_tst_ok;
+	tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
 	tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
 	return build1 (code, type, op0);
       }
@@ -20130,7 +20147,10 @@ tsubst_copy_and_build (tree t,
 	tree type;
 	tree op, r = NULL_TREE;
 
-	type = tsubst (TREE_TYPE (t), args, complain, in_decl);
+	tsubst_flags_t tcomplain = complain;
+	if (TREE_CODE (t) == CAST_EXPR)
+	  tcomplain |= tf_tst_ok;
+	type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
 	if (integral_constant_expression_p
 	    && !cast_valid_in_integral_constant_expression_p (type))
 	  {
@@ -21090,12 +21110,15 @@ tsubst_copy_and_build (tree t,
 	vec<constructor_elt, va_gc> *n;
 	constructor_elt *ce;
 	unsigned HOST_WIDE_INT idx;
-	tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
 	bool process_index_p;
         int newlen;
         bool need_copy_p = false;
 	tree r;
 
+	tsubst_flags_t tcomplain = complain;
+	if (COMPOUND_LITERAL_P (t))
+	  tcomplain |= tf_tst_ok;
+	tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
 	if (type == error_mark_node)
 	  RETURN (error_mark_node);
 
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C
new file mode 100644
index 00000000000..462da1dcdc2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction107.C
@@ -0,0 +1,20 @@
+// PR c++/104641
+// { dg-do compile { target c++17 } }
+
+template<class T>
+struct A {
+  template<class U> struct B { B(U); };
+private:
+  template<class U> struct C { C(U); };
+};
+
+template<class T>
+void f() {
+  typename A<T>::B x = 0;
+  auto y = typename A<T>::B(0);
+  auto z = typename A<T>::B{0};
+
+  typename A<T>::C w(0); // { dg-error "private" }
+}
+
+template void f<void>();
