基于嵌套内部参数专门化模板

发布于 2024-12-29 09:22:35 字数 784 浏览 5 评论 0原文

我想根据模板的内部参数来专门化模板。我正在使用非严格的评估,这让事情变得困难。

专业化应该基于最少嵌套模式匹配。例如:

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 技术交流群。

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

发布评论

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

评论(1

秋风の叶未落 2025-01-05 09:22:35

您可以只使用模板模板参数:

template<typename T>
struct data1;

template<typename T>
struct fun1 {
  using type = data1<T>;
};

template<typename T>
struct fun2;

template<class T>
struct fun2<data1<T>>{
  using type = data1<T>;
};

template<template<class> class X, class T>
struct fun2<X<T>>
  : fun2<typename X<T>::type>{};

测试:

#include <type_traits>

static_assert(std::is_same<fun2<data1<int>>::type, data1<int>>::value, "fun2<data1<int>>");
static_assert(std::is_same<fun2<data1<fun1<int>>>::type, data1<fun1<int>>>::value, "fun2<data1<fun1<int>>>");
static_assert(std::is_same<fun2<fun1<int>>::type, data1<int>>::value, "fun2<fun1<int>>");
static_assert(std::is_same<fun2<fun2<fun1<int>>>::type, data1<int>>::value, "fun2<fun2<fun1<int>>>");

int main(){
}

Ideone 上的实时示例(使用别名更改为 typedef)

You can just use a template template parameter:

template<typename T>
struct data1;

template<typename T>
struct fun1 {
  using type = data1<T>;
};

template<typename T>
struct fun2;

template<class T>
struct fun2<data1<T>>{
  using type = data1<T>;
};

template<template<class> class X, class T>
struct fun2<X<T>>
  : fun2<typename X<T>::type>{};

Tests:

#include <type_traits>

static_assert(std::is_same<fun2<data1<int>>::type, data1<int>>::value, "fun2<data1<int>>");
static_assert(std::is_same<fun2<data1<fun1<int>>>::type, data1<fun1<int>>>::value, "fun2<data1<fun1<int>>>");
static_assert(std::is_same<fun2<fun1<int>>::type, data1<int>>::value, "fun2<fun1<int>>");
static_assert(std::is_same<fun2<fun2<fun1<int>>>::type, data1<int>>::value, "fun2<fun2<fun1<int>>>");

int main(){
}

Live example on Ideone (with using-aliases changed to typedefs).

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