提取模板中的基础类型

huangapple go评论67阅读模式
英文:

Extracting the underlying type in the template

问题

I am new to C++20. The intention here is to have a template class which has value whose type would be the underlying type of T that's passed in.

So in case of T being:

  • std::optional<char>, it's char value
  • int, it's just int value.
  1. Is there any better way to extract the types than through struct TypeExtract? More or a generic solution in C++20 perhaps? Given if the class could take more than just std::optional<int> or just a primitive type?

  2. Can the condition in foo be improved specially with the way val is initialized?

英文:

I am new to C++20. The intention here is to have a template class which has value whose type would be the underlying type of T that's passed in.

So in case of T being:

  • std::optional&lt;char&gt;, it's char value
  • int, it's just int value.
  1. Is there any better way to extract the types than through struct TypeExtract? More or a generic solution in C++20 perhaps? Given if the class could take more than just std::optional&lt;int&gt; or just a primitive type?

  2. Can the condition in foo be improved specially with the way val is initialized?

template&lt;typename T&gt;
constexpr bool is_optional = false;

template&lt;typename T&gt;
constexpr bool is_optional&lt;std::optional&lt;T&gt;&gt; = true;


template&lt;typename T&gt;
struct TypeExtract 
{
    using type = T;
};

template&lt;typename T&gt;
struct TypeExtract&lt;std::optional&lt;T&gt;&gt;
{
    using type = T;
};

template &lt;typename T&gt;
concept is_integral = std::is_integral_v&lt;typename TypeExtract&lt;T&gt;::type&gt;;

template &lt;is_integral T&gt;
class A
{
    using Type = typename TypeExtract&lt;T&gt;::type;
    Type val;

    void foo(T value)
    { 
      if constexpr (is_optional&lt;T&gt;)
      {
        val = *value;
      } 
      else
      {
        val = value;
      }
    }
};


int main()
{
   A&lt;char&gt; a1;
   A&lt;std::optional&lt;int&gt;&gt; a2;
   // A&lt;double&gt; a3; // fails
}

答案1

得分: 1

代码部分不翻译,以下是翻译好的内容:

It looks like you're trying to extract first template parameter from a class template and keep on unwinding templates until you get to a non-template type. In that case you could make a type trait that is specialized for types instantiated from templates:

你似乎想要从类模板中提取第一个模板参数,并继续展开模板,直到达到非模板类型。在这种情况下,你可以创建一个专门用于从模板实例化的类型的类型特征:

You could then use it like so:

然后你可以像这样使用它:

英文:

It looks like you're trying to extract first template parameter from a class template and keep on unwinding templates until you get to a non-template type. In that case you could make a type trait that is specialized for types instantiated from templates:

// primary template
template&lt;class T, class...&gt;
struct type {
    using value_type = T;
};

// specialization for template instantiated types
template&lt;template&lt;class, class...&gt; class T, class F, class... Rest&gt;
struct type&lt;T&lt;F, Rest...&gt;&gt; {
    using value_type = typename type&lt;F&gt;::value_type;
};

// helper alias
template&lt;class... Ts&gt;
using type_t = typename type&lt;Ts...&gt;::value_type;

You could then use it like so:

int main() {
   type_t&lt;char&gt; a1;
   type_t&lt;std::optional&lt;int&gt;&gt; a2;
   type_t&lt;double, int&gt; a3;

   static_assert(std::is_same_v&lt;decltype(a1), char&gt;);
   static_assert(std::is_same_v&lt;decltype(a2), int&gt;);
   static_assert(std::is_same_v&lt;decltype(a3), double&gt;);
}

答案2

得分: 0

以下是翻译的内容:

这里没有好坏之分,这是一种风格和约定的问题,但个人认为可以去掉if constexpr,利用尾随要求来减少函数的圈复杂度。另一方面,这会增加一些样板代码。选择权在你手里。

关于类型提取,虽然不太重要,但我可能会使用一个模板基类,并将其成员导入类中,而不是将类型导入类中。这对我来说更符合惯例。

至于概念,我可能会在类型特征的位置使用更多库提供的概念。

附注:在从可选类型赋值时,考虑使用assert.value()或类似的函数,以确保它不为空。

总的来说,我可能会以以下方式编写你的代码:

#include <concepts>
#include <type_traits>
#include <optional>

template<typename T>
concept StdOptional = std::same_as<std::optional<typename T::value_type>, T>;

template<typename T>
concept OptionalIntegral = StdOptional<T> and std::integral<typename T::value_type>;

template<typename T>
concept OptionalOrOptionalIntegral = std::integral<T> or OptionalIntegral<T>;

template<typename>
struct ABase;

template<std::integral T>
struct ABase<T>
{
    T value;
};

template<OptionalIntegral T>
struct ABase<T>
{
    typename T::value_type value;
};

template<OptionalOrOptionalIntegral T>
class A : ABase<T>
{
    using ABase<T>::value;

public:
    void setValue(T val) requires(std::integral<T>)
    {
        value = val;
    }

    void setValue(T val) requires(OptionalIntegral<T>)
    {
        value = val.value();
    }
};

演示:https://godbolt.org/z/dzvr9xbGr

英文:

There is no good or bad here, it's a matter of style and convention, but personally I would get rid of if constexpr and take advantage of trailing requires for the sake of reducing function's cyclomatic complexity. On the other hand, that add some boilerplate. The choice is yours.

Not much can be done about type extraction, though I would probably use a templated base and import its member(s) instead of importing the type into the class. Not a big deal, but it feels more idiomatic to me.

As for concepts, I'd probably use more library-provided ones in the place of type traits.

Side note: consider using assert, .value() or similar function when assigning from the optional to ensure it's not empty.

All in all, I'd probably write your code somewhat this way:

#include &lt;concepts&gt;
#include &lt;type_traits&gt;
#include &lt;optional&gt;

template&lt;typename T&gt;
concept StdOptional = std::same_as&lt;std::optional&lt;typename T::value_type&gt;, T&gt;;

template&lt;typename T&gt;
concept OptionalIntegral = StdOptional&lt;T&gt; and std::integral&lt;typename T::value_type&gt;;

template&lt;typename T&gt;
concept OptionalOrOptionalIntegral = std::integral&lt;T&gt; or OptionalIntegral&lt;T&gt;;

template&lt;typename&gt;
struct ABase;

template&lt;std::integral T&gt;
struct ABase&lt;T&gt;
{
    T value;
};

template&lt;OptionalIntegral T&gt;
struct ABase&lt;T&gt;
{
    typename T::value_type value;
};

template&lt;OptionalOrOptionalIntegral T&gt;
class A : ABase&lt;T&gt;
{
    using ABase&lt;T&gt;::value;

public:
    void setValue(T val) requires(std::integral&lt;T&gt;)
    {
        value = val;
    }

    void setValue(T val) requires(OptionalIntegral&lt;T&gt;)
    {
        value = val.value();
    }
};


Demo: https://godbolt.org/z/dzvr9xbGr

huangapple
  • 本文由 发表于 2023年2月7日 03:23:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定