匹配模板模板类型参数
假设我想键入具有特定成员(约束)的任何容器,但也将类型变量绑定到容器和成员。例如,令t
和u
为模板类型变量,对应于类和成员someclass.member
。这可能吗?
让我们通过模板来简化事情。这将成员类型作为模板类型参数移动到类型系统中,该参数允许删除约束。现在,我可以将类型变量绑定到模板类型和模板类型参数吗?例如,令t
和u
是模板类型变量,与模板的容器t< u>
相对应。这可能吗?
例如,
template <typename T>
struct S1 {
T a;
int b;
};
struct S2 {
S1<int> a;
S1<double> b;
};
void cb(auto*);
// Many things have been tried
template<T<U>>
template<template<typename U> typename T>
void caller(T<U>*, void (*)(U*));
template<S1<T>>
void caller(S1<T>*, void (*)(T*));
template<T>
void caller<S1<T>>(S1<T>*, void (*)(T*));
Say I would like to type-match any container with a specific member (a constraint) - but also bind type variables to both the container and the member. For example let T
and U
be the template type variables corresponding to class and member Someclass.member
. Is this possible?
Let's simplify things by templating the container. This moves the member type into the type system as the template type parameter which allows removing the constraint. Now can I bind the type variables to both the template type and the template type parameter? For example let T
and U
be the template type variables corresponding to the templated container T<U>
. Is this possible?
For example,
template <typename T>
struct S1 {
T a;
int b;
};
struct S2 {
S1<int> a;
S1<double> b;
};
void cb(auto*);
// Many things have been tried
template<T<U>>
template<template<typename U> typename T>
void caller(T<U>*, void (*)(U*));
template<S1<T>>
void caller(S1<T>*, void (*)(T*));
template<T>
void caller<S1<T>>(S1<T>*, void (*)(T*));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
caller
的模板列表应为demo
The
caller
's template list should beDemo
这样的类型?
这里
type_identity_t
需要停止从第二个参数推导出t
,这会使编译器通过lambda时混淆。Like this?
Here
type_identity_t
is needed to stopT
from being deduced from the second argument, which confuses the compiler when passing a lambda.