https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125394
Bug ID: 125394
Summary: Builting to create reference to unknown of given type
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: tkaminsk at gcc dot gnu.org
Target Milestone: ---
For the library, it would be valuable to have a compile-time only builtin (e.g.
__builtin_unknown_ref(T)) that would be produce a reference to unknown object
of type T.
This would allow to implement function like static_size<R>() for range, as
follows:
template<typename R>
consteval range_distance_t<R>
static_size()
{
return ranges::size(__builtin_unknown_ref(T));
// Could also require something like, if that would be simpler
T& t = __builtin_unknown_ref(T);
return rangs::size(r);
}
The current implementation strategies requires instantiating lambda and class
template:
```
template<__static_sized_range _Tp>
consteval range_size_t<_Tp>
__static_size()
{
auto __conjure = [](_Tp& __t)
{ return integral_constant<range_size_t<_Tp>, ranges::size(__t)>{}; };
return decltype(__conjure(std::declval<_Tp&>()))::value;
}
```
The references returned from __builtin_unknown_ref(T) should not point for the
same object, so if the code end-up doing:
T& t1 = __builtin_unknown_ref(T);
T& t2 = __builtin_unknown_ref(T);
Then &t1 == &t2 should be false. I think both of following works:
* each call to produce new unique
* source based (call at the same location gives location based object)