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

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

Commit Message

Barnabás Pőcze July 21, 2025, 10:46 a.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>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 include/libcamera/base/details/cxx20.h | 3 +++
 1 file changed, 3 insertions(+)

Comments

Paul Elder Sept. 18, 2025, 1:25 p.m. UTC | #1
Quoting Barnabás Pőcze (2025-07-21 19:46:04)
> `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>

Looks good to me.

Reviewed-by: Paul Elder <paul.elder@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.50.1
>

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 */