基于嵌套内部参数专门化模板
我想根据模板的内部参数来专门化模板。我正在使用非严格的评估,这让事情变得困难。
专业化应该基于最少嵌套模式匹配。例如:
template<typename T>
struct data1;
template<typename T>
struct fun1 {
using type = data1<T>;
};
template<typename T>
struct fun2;
template<typename T>
struct fun2<data1<T>> {
using type = data1<T>;
};
fun2<data1<int>> x1; // this works as expected, T=int
fun2<data1<fun1<int>>>::type x2; // this works as expected, T=fun1<int>
fun2<fun1<int>>::type x3; // this should be specialized as fun2<data1<int>>, T=int
fun2<fun2<fun1<int>>>::type x4; // this should be specialized as fun2<data1<int>>, T=int
我怎样才能做到这一点?
I want to specialize templates based on their inner arguments. I am using non-strict evaluation which makes things difficult.
The specializations should be based off of the least nested pattern match. For instance:
template<typename T>
struct data1;
template<typename T>
struct fun1 {
using type = data1<T>;
};
template<typename T>
struct fun2;
template<typename T>
struct fun2<data1<T>> {
using type = data1<T>;
};
fun2<data1<int>> x1; // this works as expected, T=int
fun2<data1<fun1<int>>>::type x2; // this works as expected, T=fun1<int>
fun2<fun1<int>>::type x3; // this should be specialized as fun2<data1<int>>, T=int
fun2<fun2<fun1<int>>>::type x4; // this should be specialized as fun2<data1<int>>, T=int
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以只使用模板模板参数:
测试:
Ideone 上的实时示例(使用别名更改为 typedef)。
You can just use a template template parameter:
Tests:
Live example on Ideone (with using-aliases changed to typedefs).