diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index a40b8953e629ab3397cac8cebd1a5c5f9ae3d47e..31ff80ea6e74f32207066850cef74e2e493bca62 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -24559,7 +24559,8 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
      even if ARG == PARM, since we won't record unifications for the
      template parameters.  We might need them if we're trying to
      figure out which of two things is more specialized.  */
-  if (arg == parm && !uses_template_parms (parm))
+  if (arg == parm
+      && (DECL_P (parm) || !uses_template_parms (parm)))
     return unify_success (explain_p);
 
   /* Handle init lists early, so the rest of the function can assume
@@ -25278,11 +25279,8 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
 		    strict, explain_p);
 
     case CONST_DECL:
-      if (DECL_TEMPLATE_PARM_P (parm))
-	return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
-      if (arg != scalar_constant_value (parm))
-	return unify_template_argument_mismatch (explain_p, parm, arg);
-      return unify_success (explain_p);
+      /* CONST_DECL should already have been folded to its DECL_INITIAL.  */
+      gcc_unreachable ();
 
     case FIELD_DECL:
     case TEMPLATE_DECL:
diff --git a/gcc/testsuite/g++.dg/template/ttp41.C b/gcc/testsuite/g++.dg/template/ttp41.C
new file mode 100644
index 0000000000000000000000000000000000000000..c81e5dd240546c49667c97e852bf91c8ec4e6072
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/ttp41.C
@@ -0,0 +1,17 @@
+// PR c++/108347
+
+template<class T>
+struct A {
+  template<class U> struct C { };
+
+  template<template<class> class TT, class U>
+  struct B;
+
+  template<class U>
+  struct B<C, U*>;
+
+  template<class U>
+  struct B<C, const U*> { };
+};
+
+A<int>::B<A<int>::C, const int*> b;