[C++ patch] Reject virt-specifiers on friends and member templates
Ville Voutilainen
ville.voutilainen@gmail.com
Tue Aug 12 16:37:00 GMT 2014
On 12 August 2014 11:47, Ville Voutilainen <ville.voutilainen@gmail.com> wrote:
> We shouldn't allow friend declarations and member templates to
> have virt-specifiers, as neither of those can be virtual. Tested on
> Linux x86-64.
> Note that the standardese can be squinted at in a way that would allow
> virt-specifiers on friends, there's a DR coming for that. I do not expect
> the resolution of that DR to be anything but banning virt-specifiers
> on such declarations. Clang has very recently made a similar fix for
> friend declarations, and it already rejected virt-specifiers on member
> templates.
Minor tweak to the diagnostic for friends, say "friend declaration" instead
of "member declared as a friend". Changelog unchanged, attached for
convenience.
-------------- next part --------------
diff --git a/gcc/cp/friend.c b/gcc/cp/friend.c
index a30918c..9b37d86 100644
--- a/gcc/cp/friend.c
+++ b/gcc/cp/friend.c
@@ -426,6 +426,10 @@ do_friend (tree ctype, tree declarator, tree decl,
/* Every decl that gets here is a friend of something. */
DECL_FRIEND_P (decl) = 1;
+
+ if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
+ error ("friend declaration %qD may not have virt-specifiers",
+ decl);
/* Unfortunately, we have to handle attributes here. Normally we would
handle them in start_decl_1, but since this is a friend decl start_decl_1
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 2592172..90b2720 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -4737,6 +4737,11 @@ push_template_decl_real (tree decl, bool is_friend)
}
else if (TREE_CODE (decl) == FUNCTION_DECL)
{
+ if (member_template_p)
+ {
+ if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
+ error ("member template %qD may not have virt-specifiers", decl);
+ }
if (DECL_DESTRUCTOR_P (decl))
{
/* [temp.mem]
diff --git a/gcc/testsuite/g++.dg/cpp0x/override1.C b/gcc/testsuite/g++.dg/cpp0x/override1.C
index f5f00ee..05d7290 100644
--- a/gcc/testsuite/g++.dg/cpp0x/override1.C
+++ b/gcc/testsuite/g++.dg/cpp0x/override1.C
@@ -52,6 +52,14 @@ struct D5 : B
void D5::g() override {} // { dg-error "not allowed outside a class definition" }
void g() override {} // { dg-error "not allowed outside a class definition" }
+struct B5
+{
+ friend void f() final; // { dg-error "may not have virt-specifiers" }
+ friend void g() override; // { dg-error "may not have virt-specifiers" }
+ template <class T> void h() final; // { dg-error "may not have virt-specifiers" }
+ template <class T> void i() override; // { dg-error "may not have virt-specifiers" }
+};
+
int main()
{
D2<B> d;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: friend-member-template-virt-specifiers.changelog
Type: application/octet-stream
Size: 472 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20140812/bfa404b6/attachment.obj>
More information about the Gcc-patches
mailing list