On Thu, 26 Nov 2026 at 13:50, Jakub Jelinek <[email protected]> wrote:
>
> On Thu, Nov 26, 2026 at 01:14:41PM +0000, Jonathan Wakely wrote:
> > Or is the concern that we don't want to allow those types to be used
> > as an NTTP before C++26?
>
> Yeah, that is my concern, then all of sudden people will start depending on
> non-portable behavior in C++23 or C++20.
>
> Here is an updated patch with _GLIBCXX_STRUCTURAL macro.
>
> 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/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.
>
> --- 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/bits/c++config 2026-09-22 14:57:45.006568553
> +0200
> +++ b/libstdc++-v3/include/bits/c++config 2026-11-26 14:37:52.839653675
> +0100
> @@ -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)
>
> --- 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 14:38:56.693811682
> +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,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>
> --- a/libstdc++-v3/include/std/span 2026-06-04 08:24:37.651028042 +0200
> +++ b/libstdc++-v3/include/std/span 2026-11-26 14:39:17.084542810 +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,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
> --- a/libstdc++-v3/include/std/variant 2026-07-16 09:51:05.386686554 +0200
> +++ b/libstdc++-v3/include/std/variant 2026-11-26 14:39:26.254421892 +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,7 @@ namespace std _GLIBCXX_VISIBILITY(defaul
> _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;
> --- a/libstdc++-v3/include/std/optional 2026-08-27 10:33:18.641752179 +0200
> +++ b/libstdc++-v3/include/std/optional 2026-11-26 14:39:36.971280575 +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,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.
> --- a/libstdc++-v3/include/std/tuple 2026-11-13 14:51:48.227022419 +0100
> +++ b/libstdc++-v3/include/std/tuple 2026-11-26 14:40:05.847899806 +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,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,7 @@ _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>
Please put a line break after tuple<_T1, _T2> so that the
base-class-specifier is on the next line.
With that tweak, the library parts are OK, thanks.
It occurred to me that _GLIBCXX26_STRUCTURAL would be a better match
for how we do _GLIBCXX20_CONSTEXPR where it depends on the -std mode.
But I don't expect use to need both _GLIBCXX26_STRUCTURAL and
_GLIBCXX29_STRUCTURAL any time soon, so we should leave the "26" part
out of the macro name until we know if we need it.
> {
> typedef _Tuple_impl<0, _T1, _T2> _Inherited;
>
>
>
> Jakub
>