https://gcc.gnu.org/g:e3b0267cbed65028b6f2d7d59c5e4294fd1bfe80

commit r17-6748-ge3b0267cbed65028b6f2d7d59c5e4294fd1bfe80
Author: Jakub Jelinek <[email protected]>
Date:   Fri Nov 27 08:09:39 2026 +0100

    c++, libstdc++: Implement C++26 P3861R1 - Pragmatic approach to standard 
structural types
    
    The following patch implements this Buzios C++26 paper by adding an
    attribute to override the
    https://eel.is/c++draft/temp.param#12.3.1
    part of the structural type definition (but just the "are public and"
    part) of class types with that attribute and uses it in libstdc++
    on the class templates which ought to have that behavior in C++.
    
    In libstdc++ it defines a new macro (_GLIBCXX_STRUCTURAL) and uses
    it on some class templates.
    
    This doesn't use anything from C++29 proposed stuff like operator template,
    that definitely wouldn't be appropriate for GCC 17.
    
    2026-11-27  Jakub Jelinek  <[email protected]>
    
            P3861R1
    gcc/
            * doc/extend.texi (structural_type): Document new
            attribute.
    gcc/cp/
            * tree.cc: Implement C++26 P3861R1 - Pragmatic approach to standard
            structural types.
            (structural_type_p): Ignore TREE_PRIVATE or TREE_PROTECTED on bases
            and members on class types with gnu::structural_type attribute.
            (handle_structural_type_attribute): New function.
            (cxx_gnu_attributes): Add entry for gnu::structural_type.
    gcc/testsuite/
            * g++.dg/cpp26/structural-type1.C: New test.
            * g++.dg/ext/attr-structural-type-1.C: New test.
            * g++.dg/ext/attr-structural-type-2.C: New test.
    libstdc++-v3/
            * include/bits/version.def (structural_types): New FTM.
            * include/bits/version.h: Regenerate.
            * include/bits/c++config (_GLIBCXX_STRUCTURAL): Define.
            * include/std/string_view: Define __glibcxx_want_structural_types
            before including bits/version.h.
            (template<typename, typename> class basic_string_view): Add
            _GLIBCXX_STRUCTURAL.
            * include/std/span: Define __glibcxx_want_structural_types
            before including bits/version.h.
            (template<size_t> class __extent_storage,
            template<typename, size_t class span): Add _GLIBCXX_STRUCTURAL.
            * include/std/variant: Define __glibcxx_want_structural_types
            before including bits/version.h.
            (template<typename...> class variant): Add _GLIBCXX_STRUCTURAL.
            * include/std/optional: Define __glibcxx_want_structural_types
            before including bits/version.h.
            (template<typename> class optional): Add _GLIBCXX_STRUCTURAL.
            * include/std/tuple: Define __glibcxx_want_structural_types
            before including bits/version.h.
            (template<size_t, typename, typename...> class _Tuple_impl,
            template<size_t, typename> class _Tuple_impl,
            template<typename, typename> class tuple): Add _GLIBCXX_STRUCTURAL.
    
    Reviewed-by: Jason Merrill <[email protected]>
    Reviewed-by: Jonathan Wakely <[email protected]>

Diff:
---
 gcc/cp/tree.cc                                    | 38 ++++++++++++++++----
 gcc/doc/extend.texi                               | 12 +++++++
 gcc/testsuite/g++.dg/cpp26/structural-type1.C     | 26 ++++++++++++++
 gcc/testsuite/g++.dg/ext/attr-structural-type-1.C | 43 +++++++++++++++++++++++
 gcc/testsuite/g++.dg/ext/attr-structural-type-2.C | 30 ++++++++++++++++
 libstdc++-v3/include/bits/c++config               |  8 +++++
 libstdc++-v3/include/bits/version.def             |  9 +++++
 libstdc++-v3/include/bits/version.h               | 10 ++++++
 libstdc++-v3/include/std/optional                 |  3 +-
 libstdc++-v3/include/std/span                     |  7 ++--
 libstdc++-v3/include/std/string_view              |  3 +-
 libstdc++-v3/include/std/tuple                    | 10 +++---
 libstdc++-v3/include/std/variant                  |  3 +-
 13 files changed, 185 insertions(+), 17 deletions(-)

diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index d16fee9b0e92..c7a305f5f68c 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -5402,20 +5402,27 @@ structural_type_p (tree t, bool explain)
        explain_non_literal_class (t);
       return false;
     }
+  int structural_type_attr = -1;
   for (tree m = next_aggregate_field (TYPE_FIELDS (t)); m;
        m = next_aggregate_field (DECL_CHAIN (m)))
     {
       if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
        {
-         if (explain)
+         if (structural_type_attr == -1)
+           structural_type_attr = !!lookup_attribute ("structural_type",
+                                                      TYPE_ATTRIBUTES (t));
+         if (!structural_type_attr)
            {
-             if (DECL_FIELD_IS_BASE (m))
-               inform (location_of (m), "base class %qT is not public",
-                       TREE_TYPE (m));
-             else
-               inform (location_of (m), "%qD is not public", m);
+             if (explain)
+               {
+                 if (DECL_FIELD_IS_BASE (m))
+                   inform (location_of (m), "base class %qT is not public",
+                           TREE_TYPE (m));
+                 else
+                   inform (location_of (m), "%qD is not public", m);
+               }
+             return false;
            }
-         return false;
        }
       if (DECL_MUTABLE_P (m))
        {
@@ -5437,6 +5444,21 @@ structural_type_p (tree t, bool explain)
   return true;
 }
 
+/* Handle the [[gnu::structural_type]] attribute.  */
+
+static tree
+handle_structural_type_attribute (tree *node, tree name, tree, int,
+                                 bool *no_add_attrs)
+{
+  if (!CLASS_TYPE_P (*node))
+    {
+      warning (OPT_Wattributes, "%qE attribute can only be applied to "
+              "class types", name);
+      *no_add_attrs = true;
+    }
+  return NULL_TREE;
+}
+
 /* Partially handle the C++11 [[carries_dependency]] attribute.
    Just emit a different diagnostics when it is used on something the
    spec doesn't allow vs. where it allows and we just choose to ignore
@@ -5654,6 +5676,8 @@ static const attribute_spec cxx_gnu_attributes[] =
     handle_gnu_no_specializations_attribute, NULL },
   { "trivial_abi", 0, 0, false, true, false, true,
     handle_gnu_trivial_abi_attribute, NULL },
+  { "structural_type", 0, 0, false, true, false, false,
+    handle_structural_type_attribute, NULL }
 };
 // clang-format on
 
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 407657248fb5..6ebe10ff0c80 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -32038,6 +32038,18 @@ template <> int foo <int> () @{ return 43; @} // Error
 @};
 @end smallexample
 
+@cindex @code{structural_type} type attribute
+@item structural_type
+The @code{structural_type} attribute can be applied to a class type.
+It overrides for the given type the definition of structural types
+in C++ [temp.param], so that it doesn't require that all base classes
+and non-static data members are public.  All other requirements on
+structural types are kept intact, so the class types still have to be
+literal class types with no mutable non-static data members and that
+all base classes and non-static data members have structural types or
+arrays thereof.  This attribute is primarily meant for standard library
+class templates which are declared to be structural types by fiat
+in C++26.
 @end table
 
 @node Function Multiversioning
diff --git a/gcc/testsuite/g++.dg/cpp26/structural-type1.C 
b/gcc/testsuite/g++.dg/cpp26/structural-type1.C
new file mode 100644
index 000000000000..dc2948dd2f43
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp26/structural-type1.C
@@ -0,0 +1,26 @@
+// C++26 P3861R1 - Pragmatic approach to standard structural types
+// { dg-do compile { target c++26 } }
+
+#include <optional>
+#include <span>
+#include <string_view>
+#include <tuple>
+#include <variant>
+
+struct S { int s; };
+struct T { constexpr T () : t (0) {} private: int t; };
+static_assert (__builtin_is_structural (std::string_view));
+static_assert (__builtin_is_structural (std::basic_string_view <char>));
+static_assert (__builtin_is_structural (std::basic_string_view <wchar_t>));
+static_assert (__builtin_is_structural (std::span <int>));
+static_assert (__builtin_is_structural (std::span <int, 6>));
+static_assert (__builtin_is_structural (std::tuple <>));
+static_assert (__builtin_is_structural (std::tuple <int, long>));
+static_assert (__builtin_is_structural (std::tuple <S>));
+static_assert (!__builtin_is_structural (std::tuple <T>));
+static_assert (__builtin_is_structural (std::optional <int>));
+static_assert (__builtin_is_structural (std::optional <S>));
+static_assert (!__builtin_is_structural (std::optional <T>));
+static_assert (__builtin_is_structural (std::variant <S>));
+static_assert (__builtin_is_structural (std::variant <int, long>));
+static_assert (!__builtin_is_structural (std::variant <T>));
diff --git a/gcc/testsuite/g++.dg/ext/attr-structural-type-1.C 
b/gcc/testsuite/g++.dg/ext/attr-structural-type-1.C
new file mode 100644
index 000000000000..61a9efb39a5e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/attr-structural-type-1.C
@@ -0,0 +1,43 @@
+// C++26 P3861R1 - Pragmatic approach to standard structural types
+// { dg-do compile { target c++20 } }
+
+#if !__has_cpp_attribute(gnu::structural_type)
+#error
+#endif
+#if !__has_attribute(structural_type)
+#error
+#endif
+struct [[gnu::structural_type]] A { constexpr A () : a (0) {} private: int a; 
};
+template <typename T>
+struct [[__gnu__::__structural_type__]] B { constexpr B () : a (0) {} 
protected: T a; };
+struct [[gnu::structural_type]] C { C () : a (0) {} protected: int a; };
+template <typename T>
+struct [[gnu::structural_type]] D { D () : a (0) {} private: T a; };
+struct E { E (int x = 0) : a (x) {} private: int a; };
+struct F { constexpr F (int x = 0) : a (x) {} int a; };
+template <typename T>
+struct __attribute__((__structural_type__)) G : private T { constexpr G () : g 
(0) {} int g; };
+static_assert (__builtin_is_structural (A));
+static_assert (__builtin_is_structural (B <int>));
+static_assert (!__builtin_is_structural (B <E>));
+static_assert (__builtin_is_structural (B <F>));
+static_assert (!__builtin_is_structural (C));
+static_assert (!__builtin_is_structural (D <int>));
+static_assert (!__builtin_is_structural (D <E>));
+static_assert (!__builtin_is_structural (D <F>));
+static_assert (!__builtin_is_structural (E));
+static_assert (__builtin_is_structural (F));
+static_assert (__builtin_is_structural (G <F>));
+static_assert (!__builtin_is_structural (G <E>));
+template <auto A>
+[[gnu::noipa]] void foo () {}
+
+void
+bar ()
+{
+  foo <A {}> ();
+  foo <B <int> {}> ();
+  foo <B <F> {}> ();
+  foo <F {}> ();
+  foo <G <F> {}> ();
+}
diff --git a/gcc/testsuite/g++.dg/ext/attr-structural-type-2.C 
b/gcc/testsuite/g++.dg/ext/attr-structural-type-2.C
new file mode 100644
index 000000000000..ec2a2df8c00a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/attr-structural-type-2.C
@@ -0,0 +1,30 @@
+// C++26 P3861R1 - Pragmatic approach to standard structural types
+// { dg-do compile { target c++11 } }
+// Negative tests.
+
+struct [[gnu::structural_type]] A {
+ [[gnu::structural_type]] int &foo (int &);    // { dg-warning 
"'structural_type' attribute can only be applied to class types" }
+};
+
+[[gnu::structural_type]] int &bar (int &);     // { dg-warning 
"'structural_type' attribute can only be applied to class types" }
+
+[[gnu::structural_type]] int i;                        // { dg-warning 
"'structural_type' attribute can only be applied to class types" }
+[[gnu::structural_type]] double d;             // { dg-warning 
"'structural_type' attribute can only be applied to class types" }
+[[gnu::structural_type]] typedef int T;                // { dg-warning 
"'structural_type' attribute can only be applied to class types" }
+
+[[gnu::structural_type()]] int &fn1 (int &);   // { dg-error 
"'structural_type' attribute does not take any arguments" }
+[[gnu::structural_type("a")]] int &fn2 (int &); // { dg-error 
"'structural_type' attribute does not take any arguments" }
+[[gnu::structural_type(true, true)]] int &fn3 (int &); // { dg-error 
"'structural_type' attribute does not take any arguments" }
+
+enum [[gnu::structural_type]] E {              // { dg-warning 
"'structural_type' attribute can only be applied to class types" }
+  X [[gnu::structural_type]]                   // { dg-warning 
"'structural_type' attribute can only be applied to class types" }
+};
+
+[[gnu::structural_type]];                      // { dg-warning "ignored" }
+
+void
+g ()
+{
+  goto L;
+[[gnu::structural_type]] L:;                   // { dg-warning 
"'structural_type' attribute can only be applied to class types" }
+}
diff --git a/libstdc++-v3/include/bits/c++config 
b/libstdc++-v3/include/bits/c++config
index 0c2407ccd07c..04912633afc4 100644
--- a/libstdc++-v3/include/bits/c++config
+++ b/libstdc++-v3/include/bits/c++config
@@ -940,6 +940,14 @@ namespace __gnu_cxx
 # define _GLIBCXX_EXPLICIT_THIS_PARAMETER 202110L
 #endif
 
+// Which class types are by fiat structural in C++26 despite containing
+// private/protected bases or non-static data members.
+#if __cplusplus > 202302L && __has_cpp_attribute(__gnu__::__structural_type__)
+# define _GLIBCXX_STRUCTURAL [[__gnu__::__structural_type__]]
+#else
+# define _GLIBCXX_STRUCTURAL
+#endif
+
 // Mark code that should be ignored by the compiler, but seen by Doxygen.
 #define _GLIBCXX_DOXYGEN_ONLY(X)
 
diff --git a/libstdc++-v3/include/bits/version.def 
b/libstdc++-v3/include/bits/version.def
index d3cf2f31c307..e0a73ea3b205 100644
--- a/libstdc++-v3/include/bits/version.def
+++ b/libstdc++-v3/include/bits/version.def
@@ -2717,6 +2717,15 @@ ftms = {
   };
 };
 
+ftms = {
+  name = structural_types;
+  values = {
+    v = 202611;
+    cxxmin = 26;
+    extra_cond = "__has_cpp_attribute(__gnu__::__structural_type__)";
+  };
+};
+
 // Standard test specifications.
 stds[97] = ">= 199711L";
 stds[03] = ">= 199711L";
diff --git a/libstdc++-v3/include/bits/version.h 
b/libstdc++-v3/include/bits/version.h
index e52ab6cbdd83..700f7f776849 100644
--- a/libstdc++-v3/include/bits/version.h
+++ b/libstdc++-v3/include/bits/version.h
@@ -3001,4 +3001,14 @@
 #endif /* !defined(__cpp_lib_clmul) */
 #undef __glibcxx_want_clmul
 
+#if !defined(__cpp_lib_structural_types)
+# if (__cplusplus >  202302L) && 
(__has_cpp_attribute(__gnu__::__structural_type__))
+#  define __glibcxx_structural_types 202611L
+#  if defined(__glibcxx_want_all) || defined(__glibcxx_want_structural_types)
+#   define __cpp_lib_structural_types 202611L
+#  endif
+# endif
+#endif /* !defined(__cpp_lib_structural_types) */
+#undef __glibcxx_want_structural_types
+
 #undef __glibcxx_want_all
diff --git a/libstdc++-v3/include/std/optional 
b/libstdc++-v3/include/std/optional
index b56a3366ab76..1cf70f99a199 100644
--- a/libstdc++-v3/include/std/optional
+++ b/libstdc++-v3/include/std/optional
@@ -40,6 +40,7 @@
 #define __glibcxx_want_constrained_equality
 #define __glibcxx_want_constexpr_exceptions
 #define __glibcxx_want_hardened_optional
+#define __glibcxx_want_structural_types
 #include <bits/version.h>
 
 #ifdef __cpp_lib_optional // C++ >= 17
@@ -781,7 +782,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     * @brief Class template for optional values.
     */
   template<typename _Tp>
-    class optional
+    class _GLIBCXX_STRUCTURAL optional
     : private _Optional_base<_Tp>,
       private _Enable_copy_move<
        // Copy constructor.
diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span
index 2b978dad8a4d..c03243cb8f09 100644
--- a/libstdc++-v3/include/std/span
+++ b/libstdc++-v3/include/std/span
@@ -41,6 +41,7 @@
 #define __glibcxx_want_span
 #define __glibcxx_want_span_initializer_list
 #define __glibcxx_want_hardened_span
+#define __glibcxx_want_structural_types
 #include <bits/version.h>
 
 #ifdef __cpp_lib_span // C++ >= 20 && concepts
@@ -73,7 +74,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       inline constexpr bool __is_std_array<std::array<_Tp, _Num>> = true;
 
     template<size_t _Extent>
-      class __extent_storage
+      class _GLIBCXX_STRUCTURAL __extent_storage
       {
       public:
        // Used for runtime sizes that must satisfy the precondition.
@@ -97,7 +98,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       };
 
     template<>
-      class __extent_storage<dynamic_extent>
+      class _GLIBCXX_STRUCTURAL __extent_storage<dynamic_extent>
       {
       public:
        [[__gnu__::__always_inline__]]
@@ -120,7 +121,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   } // namespace __detail
 
   template<typename _Type, size_t _Extent = dynamic_extent>
-    class span
+    class _GLIBCXX_STRUCTURAL span
     {
       template<size_t _Offset, size_t _Count>
        static constexpr size_t
diff --git a/libstdc++-v3/include/std/string_view 
b/libstdc++-v3/include/std/string_view
index 030314a2f5be..bdb0ce6a721a 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -45,6 +45,7 @@
 #define __glibcxx_want_string_contains
 #define __glibcxx_want_string_subview
 #define __glibcxx_want_string_view
+#define __glibcxx_want_structural_types
 #include <bits/version.h>
 
 #if __cplusplus >= 201703L
@@ -110,7 +111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  @endcode
    */
   template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
-    class basic_string_view
+    class _GLIBCXX_STRUCTURAL basic_string_view
     {
       static_assert(!is_array_v<_CharT>);
       static_assert(is_trivially_copyable_v<_CharT>
diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple
index 4e28c3920b02..a582cf574670 100644
--- a/libstdc++-v3/include/std/tuple
+++ b/libstdc++-v3/include/std/tuple
@@ -54,6 +54,7 @@
 #define __glibcxx_want_ranges_zip
 #define __glibcxx_want_tuple_like
 #define __glibcxx_want_constrained_equality
+#define __glibcxx_want_structural_types
 #include <bits/version.h>
 
 namespace std _GLIBCXX_VISIBILITY(default)
@@ -239,7 +240,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    * (which contains the @c Tail).
    */
   template<size_t _Idx, typename _Head, typename... _Tail>
-    struct _Tuple_impl<_Idx, _Head, _Tail...>
+    struct _GLIBCXX_STRUCTURAL _Tuple_impl<_Idx, _Head, _Tail...>
     : public _Tuple_impl<_Idx + 1, _Tail...>,
       private _Head_base<_Idx, _Head>
     {
@@ -504,7 +505,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   // Basis case of inheritance recursion.
   template<size_t _Idx, typename _Head>
-    struct _Tuple_impl<_Idx, _Head>
+    struct _GLIBCXX_STRUCTURAL _Tuple_impl<_Idx, _Head>
     : private _Head_base<_Idx, _Head>
     {
       template<size_t, typename...> friend struct _Tuple_impl;
@@ -1950,7 +1951,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #pragma GCC diagnostic ignored "-Winvalid-specialization"
   // Explicit specialization, zero-element tuple.
   template<>
-    class tuple<>
+    class _GLIBCXX_STRUCTURAL tuple<>
     {
     public:
       // We need the default since we're going to define no-op
@@ -2029,7 +2030,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   /// Partial specialization, 2-element tuple.
   /// Includes construction and assignment from a pair.
   template<typename _T1, typename _T2>
-    class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
+    class _GLIBCXX_STRUCTURAL tuple<_T1, _T2>
+    : public _Tuple_impl<0, _T1, _T2>
     {
       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
 
diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
index 6e1388363caa..c6645a0fdae8 100644
--- a/libstdc++-v3/include/std/variant
+++ b/libstdc++-v3/include/std/variant
@@ -37,6 +37,7 @@
 #define __glibcxx_want_variant
 #define __glibcxx_want_constrained_equality
 #define __glibcxx_want_constexpr_exceptions
+#define __glibcxx_want_structural_types
 #include <bits/version.h>
 
 #ifdef __cpp_lib_variant // C++ >= 17
@@ -66,7 +67,7 @@ namespace std _GLIBCXX_VISIBILITY(default)
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   template<typename... _Types> class tuple;
-  template<typename... _Types> class variant;
+  template<typename... _Types> class _GLIBCXX_STRUCTURAL variant;
 
   template<typename _Variant>
     struct variant_size;

Reply via email to