diff mbox

[C++14] Add is_final type trait.

Message ID 5414F636.8070802@verizon.net
State New
Headers show

Commit Message

Ed Smith-Rowland Sept. 14, 2014, 1:58 a.m. UTC
We've had __has_final built-in for a good while.
the std library component is_final was added to C++14 - which is now good.
I noticed while looking at the latest SD-6 draft.

So here is a simple patch that builds and passes clean on x86_64-linux.

OK?
2014-09-14  Edward Smith-Rowland  <3dw4rd@verizon.net>

	* include/std/type_traits: Add is_final<> type trait for C++14.
	* testsuite/util/testsuite_tr1.h: Add 
	* testsuite/20_util/is_final/requirements/
	explicit_instantiation.cc: New.
	* testsuite/20_util/is_final/requirements/typedefs.cc: New.
	* testsuite/20_util/is_final/value.cc: New.
	* testsuite/20_util/declval/requirements/1_neg.cc: Adjust.
	* testsuite/20_util/make_signed/requirements/typedefs_neg.cc: Adjust.
	* testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc: Adjust.

Comments

Jonathan Wakely Sept. 14, 2014, 10:18 a.m. UTC | #1
On 13/09/14 21:58 -0400, Ed Smith-Rowland wrote:
>We've had __has_final built-in for a good while.
>the std library component is_final was added to C++14 - which is now good.
>I noticed while looking at the latest SD-6 draft.
>
>So here is a simple patch that builds and passes clean on x86_64-linux.
>
>OK?

Yes, OK for trunk. Thanks, Ed.

I'd be willing to approve a backport to 4.9 as well (changing the test
from >= 201402L to > 201103L) if you wanted to prepare that patch :-)
diff mbox

Patch

Index: include/std/type_traits
===================================================================
--- include/std/type_traits	(revision 215247)
+++ include/std/type_traits	(working copy)
@@ -634,6 +634,15 @@ 
     : public integral_constant<bool, __is_polymorphic(_Tp)>
     { };
 
+#if __cplusplus >= 201402L
+#define __cpp_lib_is_final 201402L
+  /// is_final
+  template<typename _Tp>
+    struct is_final
+    : public integral_constant<bool, __is_final(_Tp)>
+    { };
+#endif
+
   /// is_abstract
   template<typename _Tp>
     struct is_abstract
Index: testsuite/20_util/declval/requirements/1_neg.cc
===================================================================
--- testsuite/20_util/declval/requirements/1_neg.cc	(revision 215247)
+++ testsuite/20_util/declval/requirements/1_neg.cc	(working copy)
@@ -19,7 +19,7 @@ 
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-error "static assertion failed" "" { target *-*-* } 2082 }
+// { dg-error "static assertion failed" "" { target *-*-* } 2091 }
 
 #include <utility>
 
Index: testsuite/20_util/is_final/requirements/explicit_instantiation.cc
===================================================================
--- testsuite/20_util/is_final/requirements/explicit_instantiation.cc	(revision 0)
+++ testsuite/20_util/is_final/requirements/explicit_instantiation.cc	(working copy)
@@ -0,0 +1,29 @@ 
+// { dg-options "-std=gnu++14" }
+// { dg-do compile }
+
+// Copyright (C) 2014 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// NB: This file is for testing type_traits with NO OTHER INCLUDES.
+
+#include <type_traits>
+
+namespace std
+{
+  typedef short test_type;
+  template struct is_final<test_type>;
+}
Index: testsuite/20_util/is_final/requirements/typedefs.cc
===================================================================
--- testsuite/20_util/is_final/requirements/typedefs.cc	(revision 0)
+++ testsuite/20_util/is_final/requirements/typedefs.cc	(working copy)
@@ -0,0 +1,34 @@ 
+// { dg-options "-std=gnu++14" }
+// { dg-do compile }
+
+// Copyright (C) 2014 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// 
+// NB: This file is for testing type_traits with NO OTHER INCLUDES.
+
+#include <type_traits>
+
+void test01()
+{
+  // Check for required typedefs
+  typedef std::is_final<int> test_type;
+  typedef test_type::value_type               value_type;
+  typedef test_type::type                     type;
+  typedef test_type::type::value_type         type_value_type;
+  typedef test_type::type::type               type_type;
+}
Index: testsuite/20_util/is_final/value.cc
===================================================================
--- testsuite/20_util/is_final/value.cc	(revision 0)
+++ testsuite/20_util/is_final/value.cc	(working copy)
@@ -0,0 +1,35 @@ 
+// { dg-options "-std=gnu++14" }
+// { dg-do compile }
+
+// Copyright (C) 2011-2014 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <type_traits>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+  using std::is_final;
+  using namespace __gnu_test;
+
+  // Positive test.
+  static_assert(test_category<is_final, FinalType>(true), "");
+
+  // Negative tests.
+  static_assert(test_category<is_final, ClassType>(false), "");
+  static_assert(test_category<is_final, DerivedType>(false), "");
+}
Index: testsuite/20_util/make_signed/requirements/typedefs_neg.cc
===================================================================
--- testsuite/20_util/make_signed/requirements/typedefs_neg.cc	(revision 215247)
+++ testsuite/20_util/make_signed/requirements/typedefs_neg.cc	(working copy)
@@ -48,5 +48,5 @@ 
 // { dg-error "required from here" "" { target *-*-* } 40 }
 // { dg-error "required from here" "" { target *-*-* } 42 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1747 }
-// { dg-error "declaration of" "" { target *-*-* } 1711 }
+// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1756 }
+// { dg-error "declaration of" "" { target *-*-* } 1720 }
Index: testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc
===================================================================
--- testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc	(revision 215247)
+++ testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc	(working copy)
@@ -48,5 +48,5 @@ 
 // { dg-error "required from here" "" { target *-*-* } 40 }
 // { dg-error "required from here" "" { target *-*-* } 42 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1650 }
-// { dg-error "declaration of" "" { target *-*-* } 1614 }
+// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1659 }
+// { dg-error "declaration of" "" { target *-*-* } 1623 }
Index: testsuite/util/testsuite_tr1.h
===================================================================
--- testsuite/util/testsuite_tr1.h	(revision 215247)
+++ testsuite/util/testsuite_tr1.h	(working copy)
@@ -100,6 +100,10 @@ 
 
   class DerivedType : public ClassType { };
 
+#if __cplusplus >= 201103L
+  class FinalType final : public DerivedType { };
+#endif
+
   enum EnumType { e0 };
 
   struct ConvType