C++ Logo

std-proposals

Advanced search

[std-proposals] Enhance concepts to check for member field type

From: Gergely Nagy <gergely.nagy.alt_at_[hidden]>
Date: Wed, 20 Mar 2024 10:32:50 +0100
Hey,

Imagine you have two structs:

*struct* A { *int* x; };
*struct* B { *int*& x };


and you want to write a concept that can differentiate between them.

*template*<*typename* T>
*concept* IsAKind = *requires*(T t)
{
    { t.x } -> *std*::same_as<*int*>;
};


This will fail: t.x is an lvalue reference. So both A and B will satisfy
it. Removing the reference obviously does not help.

In RUNTIME you can check:

*assert*(*reinterpret_cast*<*char**>(&t.x) == *reinterpret_cast*<*char*
*>(&t));


but this won't help compile-time. It would be great to have some way to
tell if an identifier is a reference or a thing.

What do you think?

Thanks,
    Gergely

Received on 2024-03-20 09:33:03