概念中的嵌套模板结构
我有这样的结构:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
typename
需要
-prause之后是类型而不是模板。由于t :: val
是一个接受a numeric 值的类模板,因此只需实例化t :: val
带有0
应该足够或
demo
The
typename
in therequires
-clause is followed by a type rather than a template. SinceT::val
is a class template that accepts a numeric value, simply instantiatingT::val
with0
should be enoughOr
Demo