概念中的嵌套模板结构

发布于 2025-02-05 21:06:53 字数 1470 浏览 3 评论 0原文

我有这样的结构:

struct i32 {
    template<int32_t x>
    struct val {
        static constexpr int32_t v = x;
    };

    template<typename v1, typename v2>
    struct add {
        using type = val<v1::v + v2::v>;
    };

    template<typename v1, typename v2>
    using add_t = typename add<v1, v2>::type;
};

显而易见,我在该结构中还有更多的方法和嵌套类型。而且我还有其他类似的结构,例如i64等,我将其命名为“戒指”。

在代码后面,我有一个模板结构,

template<typename Ring>
struct FractionField {
// many things here
};

我想为我的环类型提供一个C ++概念,因此我可以在编译类型上检查用户定义的“环”具有适当的嵌套类型和操作。

我的尝试看起来像:


template<typename T>
concept RingConcept = requires {
    typename T::val; // how to express that val must be a template on a numeric value ?? 
    typename T::template add_t<typename T::val, typename T::val>; // same here ??
};

但是,我的尝试都没有结论性。

基本上,我总是会遇到这种错误,而不知道如何修复它们:

error: constraints not satisfied for alias template 'FractionField' [with Ring = i32]
        using Q32 = FractionField<i32>;
                 note: because 'typename T::template add_t<typename T::val, typename T::val>' would be invalid: typename specifier refers to class template member in 'i32'; argument deduction not allowed here
        typename T::template add_t<typename T::val, typename T::val>;

我希望我不需要为嵌套模板使用的每个数字类型编写一个概念val

I have struct like that :

struct i32 {
    template<int32_t x>
    struct val {
        static constexpr int32_t v = x;
    };

    template<typename v1, typename v2>
    struct add {
        using type = val<v1::v + v2::v>;
    };

    template<typename v1, typename v2>
    using add_t = typename add<v1, v2>::type;
};

Obviouly, I have many more methods and nested types in that struct. And I have also other similar structs, such as i64 and so on, which I name "Rings".

Later in the code, I have a template struct,

template<typename Ring>
struct FractionField {
// many things here
};

I'd like to have a c++ concept for my Ring type, so I can check at compile type that user defined "Rings" have the appropriate nested types and operations.

My attempt look like that :


template<typename T>
concept RingConcept = requires {
    typename T::val; // how to express that val must be a template on a numeric value ?? 
    typename T::template add_t<typename T::val, typename T::val>; // same here ??
};

However, none of my attempts have been conclusive.

Basically, I always get that kind of errors, without knowing how to fix them :

error: constraints not satisfied for alias template 'FractionField' [with Ring = i32]
        using Q32 = FractionField<i32>;
                 note: because 'typename T::template add_t<typename T::val, typename T::val>' would be invalid: typename specifier refers to class template member in 'i32'; argument deduction not allowed here
        typename T::template add_t<typename T::val, typename T::val>;

I hope I don't need to write a concept for every numeric types used for the nested template val.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在巴黎塔顶看东京樱花 2025-02-12 21:06:53

typename 需要 -prause之后是类型而不是模板。由于t :: val是一个接受a numeric 值的类模板,因此只需实例化t :: val带有0应该足够

template<typename T, typename val = typename T::template val<0>>
concept RingConcept = requires {
  typename T::template add_t<val, val>;
};

template<typename T>
concept RingConcept = requires {
  typename T::template val<0>;
  typename T::template add_t<typename T::template val<0>, 
                             typename T::template val<0>>;
};

demo

The typename in the requires-clause is followed by a type rather than a template. Since T::val is a class template that accepts a numeric value, simply instantiating T::val with 0 should be enough

template<typename T, typename val = typename T::template val<0>>
concept RingConcept = requires {
  typename T::template add_t<val, val>;
};

Or

template<typename T>
concept RingConcept = requires {
  typename T::template val<0>;
  typename T::template add_t<typename T::template val<0>, 
                             typename T::template val<0>>;
};

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文