[RFC,v1,04/23] libcamera: base: cxx20: Add `type_identity{, _t}`
diff mbox series

Message ID 20250606164156.1442682-5-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • libcamera: Add `MetadataList`
Related show

Commit Message

Barnabás Pőcze June 6, 2025, 4:41 p.m. UTC
`type_identity_t` can be used to force non-deduced contexts
in templates, such as:

  void f(T x, type_identity_t<T> y)

when calling `f(1u, 2)`, without `type_identity_t`, the compiler
could not unambiguously deduce `T` (unsigned int vs int). However,
with `type_identity_t`, the type of the argument passed to `y`
will not be used to deduce `T`, only the argument passed to `x`.

See https://en.cppreference.com/w/cpp/types/type_identity

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 include/libcamera/base/details/cxx20.h | 3 +++
 1 file changed, 3 insertions(+)

Comments

Jacopo Mondi June 11, 2025, 5:43 a.m. UTC | #1
Hi Barnabás

On Fri, Jun 06, 2025 at 06:41:37PM +0200, Barnabás Pőcze wrote:
> `type_identity_t` can be used to force non-deduced contexts
> in templates, such as:
>
>   void f(T x, type_identity_t<T> y)
>
> when calling `f(1u, 2)`, without `type_identity_t`, the compiler
> could not unambiguously deduce `T` (unsigned int vs int). However,
> with `type_identity_t`, the type of the argument passed to `y`
> will not be used to deduce `T`, only the argument passed to `x`.
>
> See https://en.cppreference.com/w/cpp/types/type_identity
>
> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>

Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>

> ---
>  include/libcamera/base/details/cxx20.h | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/include/libcamera/base/details/cxx20.h b/include/libcamera/base/details/cxx20.h
> index 0d6173d1b..2d26db53e 100644
> --- a/include/libcamera/base/details/cxx20.h
> +++ b/include/libcamera/base/details/cxx20.h
> @@ -9,4 +9,7 @@
>
>  namespace libcamera::details::cxx20 {
>
> +template<typename T> struct type_identity { using type = T; };
> +template<typename T> using type_identity_t = typename type_identity<T>::type;
> +
>  } /* namespace libcamera::details::cxx20 */
> --
> 2.49.0
>

Patch
diff mbox series

diff --git a/include/libcamera/base/details/cxx20.h b/include/libcamera/base/details/cxx20.h
index 0d6173d1b..2d26db53e 100644
--- a/include/libcamera/base/details/cxx20.h
+++ b/include/libcamera/base/details/cxx20.h
@@ -9,4 +9,7 @@ 
 
 namespace libcamera::details::cxx20 {
 
+template<typename T> struct type_identity { using type = T; };
+template<typename T> using type_identity_t = typename type_identity<T>::type;
+
 } /* namespace libcamera::details::cxx20 */