On Thu, 26 Nov 2026 at 12:15, Jakub Jelinek <[email protected]> wrote:
>
> Hi!
>
> 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++ do you want some bits/c++config.h macro that could simplify

Yes, I think so.

> it (though, in that case I guess it would need to be guarded there
> on the __has_cpp_attribute and C++26 or later separately, couldn't use
> the FTM to decide).

Right, <bits/version.h> isn't included until after bits/c++config.h.
But does it matter if we use the attribute before C++26? Shouldn't it
be harmless as long as the [[xxx]] syntax is valid, which is true in
all of <optional>, <string_view>, <span>, <tuple>, and <variant>.

So we could just do:

#if __has_cpp_attribute(__gnu__::__structural_type__)
# define _GLIBCXX_STRUCTURAL [[__gnu__::__structural_type__]]
#else
# define _GLIBCXX_STRUCTURAL
#endif

Or is the concern that we don't want to allow those types to be used
as an NTTP before C++26?
It looks like with this patch the front-end allows something with the
attribute to be an NTTP before C++26, but we don't want to do that for
the std types?

Then I guess you're right, we need to do:

#if __cplusplus > 202302L && __has_cpp_attribute(__gnu__::__structural_type__)
# define _GLIBCXX_STRUCTURAL [[__gnu__::__structural_type__]]
#else
# define _GLIBCXX_STRUCTURAL
#endif


>  If so, how would you like to spell it?

I like _GLIBCXX_STRUCTURAL.

I don't think we need a _TYPE suffix if it occurs straight after the
'class' or 'struct' keyword).



>
> This doesn't use anything from C++29 proposed stuff like operator template,
> that definitely wouldn't be appropriate for GCC 17.
>
> So far lightly tested on x86_64-linux.
>
> 2026-11-26  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/std/string_view: Define __glibcxx_want_structural_types
>         before including bits/version.h.
>         (template<typename, typename> class basic_string_view): Add
>         [[__gnu__::__structural_type__]] for C++26.
>         * 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
>         [[__gnu__::__structural_type__]] for C++26.
>         * include/std/variant: Define __glibcxx_want_structural_types
>         before including bits/version.h.
>         (template<typename...> class variant): Add
>         [[__gnu__::__structural_type__]] for C++26.
>         * include/std/optional: Define __glibcxx_want_structural_types
>         before including bits/version.h.
>         (template<typename> class optional): Add
>         [[__gnu__::__structural_type__]] for C++26.
>         * 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
>         [[__gnu__::__structural_type__]] for C++26.
>
> --- a/gcc/doc/extend.texi       2026-11-26 09:38:28.097796823 +0100
> +++ b/gcc/doc/extend.texi       2026-11-26 12:56:27.276381736 +0100
> @@ -32038,6 +32038,18 @@ template <> int foo <int> () @{ return 4
>  @};
>  @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
> --- a/gcc/cp/tree.cc    2026-11-16 10:57:08.810220224 +0100
> +++ b/gcc/cp/tree.cc    2026-11-26 11:49:57.918276443 +0100
> @@ -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_attr
>      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
>
> --- a/gcc/testsuite/g++.dg/cpp26/structural-type1.C     2026-11-26 
> 12:33:50.741407595 +0100
> +++ b/gcc/testsuite/g++.dg/cpp26/structural-type1.C     2026-11-26 
> 12:34:50.163618404 +0100
> @@ -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>));
> --- a/gcc/testsuite/g++.dg/ext/attr-structural-type-1.C 2026-11-26 
> 12:10:11.084138977 +0100
> +++ b/gcc/testsuite/g++.dg/ext/attr-structural-type-1.C 2026-11-26 
> 12:11:54.041784868 +0100
> @@ -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> {}> ();
> +}
> --- a/gcc/testsuite/g++.dg/ext/attr-structural-type-2.C 2026-11-26 
> 12:12:06.067626701 +0100
> +++ b/gcc/testsuite/g++.dg/ext/attr-structural-type-2.C 2026-11-26 
> 12:17:21.569477188 +0100
> @@ -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" }
> +}
> --- a/libstdc++-v3/include/bits/version.def     2026-11-21 14:09:12.607390905 
> +0100
> +++ b/libstdc++-v3/include/bits/version.def     2026-11-26 12:20:15.489189780 
> +0100
> @@ -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";
> --- a/libstdc++-v3/include/bits/version.h       2026-11-21 14:09:12.609666216 
> +0100
> +++ b/libstdc++-v3/include/bits/version.h       2026-11-26 12:20:18.468647554 
> +0100
> @@ -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
> --- a/libstdc++-v3/include/std/string_view      2026-10-15 11:04:01.399021768 
> +0200
> +++ b/libstdc++-v3/include/std/string_view      2026-11-26 12:24:03.461191475 
> +0100
> @@ -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,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>     *  @endcode
>     */
>    template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
> -    class basic_string_view
> +    class
> +#ifdef __glibcxx_structural_types
> +    [[__gnu__::__structural_type__]]
> +#endif
> +    basic_string_view
>      {
>        static_assert(!is_array_v<_CharT>);
>        static_assert(is_trivially_copyable_v<_CharT>
> --- a/libstdc++-v3/include/std/span     2026-06-04 08:24:37.651028042 +0200
> +++ b/libstdc++-v3/include/std/span     2026-11-26 12:45:38.413007192 +0100
> @@ -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,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>        inline constexpr bool __is_std_array<std::array<_Tp, _Num>> = true;
>
>      template<size_t _Extent>
> -      class __extent_storage
> +      class
> +#ifdef __glibcxx_structural_types
> +      [[__gnu__::__structural_type__]]
> +#endif
> +      __extent_storage
>        {
>        public:
>         // Used for runtime sizes that must satisfy the precondition.
> @@ -97,7 +102,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>        };
>
>      template<>
> -      class __extent_storage<dynamic_extent>
> +      class
> +#ifdef __glibcxx_structural_types
> +      [[__gnu__::__structural_type__]]
> +#endif
> +      __extent_storage<dynamic_extent>
>        {
>        public:
>         [[__gnu__::__always_inline__]]
> @@ -120,7 +129,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>    } // namespace __detail
>
>    template<typename _Type, size_t _Extent = dynamic_extent>
> -    class span
> +    class
> +#ifdef __glibcxx_structural_types
> +    [[__gnu__::__structural_type__]]
> +#endif
> +    span
>      {
>        template<size_t _Offset, size_t _Count>
>         static constexpr size_t
> --- a/libstdc++-v3/include/std/variant  2026-07-16 09:51:05.386686554 +0200
> +++ b/libstdc++-v3/include/std/variant  2026-11-26 12:28:08.648950944 +0100
> @@ -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,12 @@ namespace std _GLIBCXX_VISIBILITY(defaul
>  _GLIBCXX_BEGIN_NAMESPACE_VERSION
>
>    template<typename... _Types> class tuple;
> -  template<typename... _Types> class variant;
> +  template<typename... _Types>
> +  class
> +#ifdef __glibcxx_structural_types
> +    [[__gnu__::__structural_type__]]
> +#endif
> +  variant;
>
>    template<typename _Variant>
>      struct variant_size;
> --- a/libstdc++-v3/include/std/optional 2026-08-27 10:33:18.641752179 +0200
> +++ b/libstdc++-v3/include/std/optional 2026-11-26 12:29:39.620742743 +0100
> @@ -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,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>      * @brief Class template for optional values.
>      */
>    template<typename _Tp>
> -    class optional
> +    class
> +#ifdef __glibcxx_structural_types
> +    [[__gnu__::__structural_type__]]
> +#endif
> +    optional
>      : private _Optional_base<_Tp>,
>        private _Enable_copy_move<
>         // Copy constructor.
> --- a/libstdc++-v3/include/std/tuple    2026-11-13 14:51:48.227022419 +0100
> +++ b/libstdc++-v3/include/std/tuple    2026-11-26 12:47:14.298732565 +0100
> @@ -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,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>     * (which contains the @c Tail).
>     */
>    template<size_t _Idx, typename _Head, typename... _Tail>
> -    struct _Tuple_impl<_Idx, _Head, _Tail...>
> +    struct
> +#ifdef __glibcxx_structural_types
> +    [[__gnu__::__structural_type__]]
> +#endif
> +    _Tuple_impl<_Idx, _Head, _Tail...>
>      : public _Tuple_impl<_Idx + 1, _Tail...>,
>        private _Head_base<_Idx, _Head>
>      {
> @@ -504,7 +509,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>
>    // Basis case of inheritance recursion.
>    template<size_t _Idx, typename _Head>
> -    struct _Tuple_impl<_Idx, _Head>
> +    struct
> +#ifdef __glibcxx_structural_types
> +    [[__gnu__::__structural_type__]]
> +#endif
> +    _Tuple_impl<_Idx, _Head>
>      : private _Head_base<_Idx, _Head>
>      {
>        template<size_t, typename...> friend struct _Tuple_impl;
> @@ -1950,7 +1959,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  #pragma GCC diagnostic ignored "-Winvalid-specialization"
>    // Explicit specialization, zero-element tuple.
>    template<>
> -    class tuple<>
> +    class
> +#ifdef __glibcxx_structural_types
> +    [[__gnu__::__structural_type__]]
> +#endif
> +    tuple<>
>      {
>      public:
>        // We need the default since we're going to define no-op
> @@ -2029,7 +2042,11 @@ _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
> +#ifdef __glibcxx_structural_types
> +    [[__gnu__::__structural_type__]]
> +#endif
> +    tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
>      {
>        typedef _Tuple_impl<0, _T1, _T2> _Inherited;
>
>
>         Jakub
>

Reply via email to