C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Type trait std::is_structural

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Fri, 25 Apr 2025 19:58:21 -0400
A quick Google search for "is_structural wg21" turns up:
https://github.com/cplusplus/papers/issues/139 — P1154 "Type traits for
structural equality"
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1154r2.html>,
coauthored by Jeff Snyder and myself. I forget if I ever knew the details;
but it looks like we got this into C++20 and then it was removed again as a
result of
https://cplusplus.github.io/LWG/issue3354
and neither of us were paying attention by then, so that revert never got
un-reverted.
Meanwhile, `std::is_structural` is mentioned (and I *think* proposed? it's
hard to tell at a quick glance) in P3380R1 "Extending support for class
types as non-type template parameters"
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3380r1.html> (Barry
Revzin, December 2024) — FWIW, since I don't follow Reflection, I don't
understand the exact connection between that paper (which seems to be
deeply involved with Reflection) and the idea of adding a
std::is_structural{,_type} trait to the "normal" <type_traits> header. But
it's definitely mentioned in that paper for *some* reason.

Anyway, yeah, there's tons of history here; but I'm afraid I don't remember
(or never knew) what all the history *is*.

One possible problem with `is_structural` is that it's like the old
now-removed `is_literal_type` — it tells you something about the type, but
not a useful thing. Suppose I know that `T` is structural. Does that mean
that I can write `MyTemplate<T{}>`? No, of course not: `T` might not have a
constexpr default constructor. Or a constexpr destructor. Or its constexpr
default constructor might set its non-static data members to something that
can't be mangled (such as the address of a string literal). Or something
else I'm not thinking of. So, if you can't use `is_structural` to tell you
anything usable about the type, what exactly *is* the point of it?

Contrariwise, if all you want to know is whether `MyTemplate<T{}>` will
compile, I *think in practice* you can (generally) use SFINAE or a
requires-expression to tell you that, even without a dedicated trait.

–Arthur

On Fri, Apr 25, 2025 at 7:28 PM Braden Ganetsky via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hi all,
>
> I haven't posted to this list before. I searched the archive to see if
> this topic has been discussed here before, and I couldn't find anything.
>
> In C++20 we can now take class types as non-type template parameters, as
> long as it is a "structural type". In my own code, I've wanted to directly
> check this property at compile-time. Preferably, I'd like to static_assert
> this after the definition of my type, instead of it simply failing to work
> in the NTTP.
>
> Since "structural type" is defined in the standard (
> https://eel.is/c++draft/temp.param#11) and not just a colloquial term, I
> would like to see a trait that corresponds to this definition,
> "std::is_structural". Would anyone else see value in this? Has this been
> discussed before somewhere?
>
> Thanks,
> Braden
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-04-25 23:58:36