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

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

Commit Message

Barnabás Pőcze Oct. 30, 2025, 4:57 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>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
---
 include/libcamera/base/internal/cxx20.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

Comments

Kieran Bingham Oct. 31, 2025, 11:18 a.m. UTC | #1
Quoting Barnabás Pőcze (2025-10-30 16:57:58)
> `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>
> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>

I'd usually skip over patches with two RB tags already but this one is
interesting to me as I've been spending more time with templates lately
- and so I think this is a nice addition.


Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  include/libcamera/base/internal/cxx20.h | 24 ++++++++++++++++++++++++
>  1 file changed, 24 insertions(+)
> 
> diff --git a/include/libcamera/base/internal/cxx20.h b/include/libcamera/base/internal/cxx20.h
> index 70a43f83c9..1f4caf56f8 100644
> --- a/include/libcamera/base/internal/cxx20.h
> +++ b/include/libcamera/base/internal/cxx20.h
> @@ -15,4 +15,28 @@
>  
>  namespace libcamera::internal::cxx20 {
>  
> +/**
> + * \internal
> + * \brief std::type_identity
> + *
> + * Implementation of std::type_identity for C++17.
> + */
> +template<typename T> struct type_identity {
> +       /**
> +        * \internal
> +        * \brief std::type_identity<T>::type
> +        *
> +        * Type alias matching the template parameter.
> +        */
> +       using type = T;
> +};
> +
> +/**
> + * \internal
> + * \brief std::type_identity_t
> + *
> + * Implementation of std::type_identity_t for C++17.
> + */
> +template<typename T> using type_identity_t = typename type_identity<T>::type;
> +
>  } /* namespace libcamera::internal::cxx20 */
> -- 
> 2.51.1
>

Patch
diff mbox series

diff --git a/include/libcamera/base/internal/cxx20.h b/include/libcamera/base/internal/cxx20.h
index 70a43f83c9..1f4caf56f8 100644
--- a/include/libcamera/base/internal/cxx20.h
+++ b/include/libcamera/base/internal/cxx20.h
@@ -15,4 +15,28 @@ 
 
 namespace libcamera::internal::cxx20 {
 
+/**
+ * \internal
+ * \brief std::type_identity
+ *
+ * Implementation of std::type_identity for C++17.
+ */
+template<typename T> struct type_identity {
+	/**
+	 * \internal
+	 * \brief std::type_identity<T>::type
+	 *
+	 * Type alias matching the template parameter.
+	 */
+	using type = T;
+};
+
+/**
+ * \internal
+ * \brief std::type_identity_t
+ *
+ * Implementation of std::type_identity_t for C++17.
+ */
+template<typename T> using type_identity_t = typename type_identity<T>::type;
+
 } /* namespace libcamera::internal::cxx20 */