Index: gcc/gcc/cp/cp-tree.h
===================================================================
--- gcc.orig/gcc/cp/cp-tree.h	2011-01-13 11:59:19.540648100 +0100
+++ gcc/gcc/cp/cp-tree.h	2011-01-13 12:02:31.323125500 +0100
@@ -1221,9 +1221,9 @@ enum languages { lang_c, lang_cplusplus,
 
 /* Gives the visibility specification for a class type.  */
 #define CLASSTYPE_VISIBILITY(TYPE)		\
-	DECL_VISIBILITY (TYPE_NAME (TYPE))
+	DECL_VISIBILITY (TYPE_MAIN_DECL (TYPE))
 #define CLASSTYPE_VISIBILITY_SPECIFIED(TYPE)	\
-	DECL_VISIBILITY_SPECIFIED (TYPE_NAME (TYPE))
+	DECL_VISIBILITY_SPECIFIED (TYPE_MAIN_DECL (TYPE))
 
 typedef struct GTY (()) tree_pair_s {
   tree purpose;
Index: gcc/gcc/cp/decl2.c
===================================================================
--- gcc.orig/gcc/cp/decl2.c	2011-01-13 11:56:09.576177000 +0100
+++ gcc/gcc/cp/decl2.c	2011-01-13 12:02:31.323125500 +0100
@@ -2073,7 +2073,8 @@ determine_visibility (tree decl)
 	  tree underlying_type = TREE_TYPE (DECL_NAME (decl));
 	  int underlying_vis = type_visibility (underlying_type);
 	  if (underlying_vis == VISIBILITY_ANON
-	      || CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type))
+	      || (CLASS_TYPE_P (underlying_type)
+		  && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
 	    constrain_visibility (decl, underlying_vis);
 	  else
 	    DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
Index: gcc/gcc/testsuite/g++.dg/ext/pr47213.C
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ gcc/gcc/testsuite/g++.dg/ext/pr47213.C	2011-01-13 12:02:31.323125500 +0100
@@ -0,0 +1,15 @@
+// { dg-do compile }
+// { dg-options "-fvisibility-ms-compat" }
+#include <typeinfo>
+
+template < typename T > void
+bar ()
+{
+  typeid (T);
+}
+
+void
+foo ()
+{
+  bar < int () > ();
+}
Index: gcc/gcc/config/i386/cygming.h
===================================================================
--- gcc.orig/gcc/config/i386/cygming.h	2011-01-13 11:56:09.569177000 +0100
+++ gcc/gcc/config/i386/cygming.h	2011-01-13 12:02:31.323125500 +0100
@@ -465,6 +465,9 @@ do {						\
 #define TARGET_CXX_ADJUST_CLASS_AT_DEFINITION i386_pe_adjust_class_at_definition
 #define TARGET_MANGLE_DECL_ASSEMBLER_NAME i386_pe_mangle_decl_assembler_name
 
+#undef TARGET_ASM_ASSEMBLE_VISIBILITY
+#define TARGET_ASM_ASSEMBLE_VISIBILITY i386_pe_assemble_visibility
+
 /* Static stack checking is supported by means of probes.  */
 #define STACK_CHECK_STATIC_BUILTIN 1
 
Index: gcc/gcc/config/i386/i386-protos.h
===================================================================
--- gcc.orig/gcc/config/i386/i386-protos.h	2011-01-13 11:56:09.570177000 +0100
+++ gcc/gcc/config/i386/i386-protos.h	2011-01-13 12:02:31.338750600 +0100
@@ -225,6 +225,7 @@ extern void i386_pe_asm_output_aligned_d
 extern void i386_pe_file_end (void);
 extern void i386_pe_start_function (FILE *, const char *, tree);
 extern void i386_pe_end_function (FILE *, const char *, tree);
+extern void i386_pe_assemble_visibility (tree, int);
 extern tree i386_pe_mangle_decl_assembler_name (tree, tree);
 extern tree i386_pe_mangle_assembler_name (const char *);
 
Index: gcc/gcc/config/i386/winnt.c
===================================================================
--- gcc.orig/gcc/config/i386/winnt.c	2011-01-13 11:56:09.572177000 +0100
+++ gcc/gcc/config/i386/winnt.c	2011-01-13 12:02:31.338750600 +0100
@@ -232,6 +232,22 @@ i386_pe_maybe_mangle_decl_assembler_name
   return new_id;
 }
 
+/* Emit an assembler directive to set symbol for DECL visibility to
+   the visibility type VIS, which must not be VISIBILITY_DEFAULT.
+   As for PE there is no hidden support in gas, we just warn for
+   user-specified visibility attributes.  */
+
+void
+i386_pe_assemble_visibility (tree decl,
+			     int vis ATTRIBUTE_UNUSED)
+{
+  if (!decl
+      || !lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)))
+    return;
+  warning (OPT_Wattributes, "visibility attribute not supported "
+	   "in this configuration; ignored");
+}
+
 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
    in the language-independent default hook
    langhooks,c:lhd_set_decl_assembler_name ()
