匹配模板模板类型参数

发布于 2025-02-02 17:36:07 字数 780 浏览 4 评论 0原文

假设我想键入具有特定成员(约束)的任何容器,但也将类型变量绑定到容器和成员。例如,令tu为模板类型变量,对应于类和成员someclass.member。这可能吗?

让我们通过模板来简化事情。这将成员类型作为模板类型参数移动到类型系统中,该参数允许删除约束。现在,我可以将类型变量绑定到模板类型和模板类型参数吗?例如,令tu是模板类型变量,与模板的容器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 技术交流群。

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

发布评论

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

评论(2

伴随着你 2025-02-09 17:36:07

caller的模板列表应为

template <typename T>
struct S1 {
  T a;
  int b;
};

void cb(auto*);

template<template<typename...> typename Temp, typename U>
void caller(Temp<U>*, void (*)(U*));

int main() {
  S1<int> a;
  caller(&a, cb);

  S1<double> b;
  caller(&b, cb);
}

demo

The caller's template list should be

template <typename T>
struct S1 {
  T a;
  int b;
};

void cb(auto*);

template<template<typename...> typename Temp, typename U>
void caller(Temp<U>*, void (*)(U*));

int main() {
  S1<int> a;
  caller(&a, cb);

  S1<double> b;
  caller(&b, cb);
}

Demo

演多会厌 2025-02-09 17:36:07

我想传递s1指针s1cb到呼叫者,以便cb的类型是从s1-&gt; a

的类型推断

这样的类型?

#include <iostream>
#include <type_traits>

template <typename T>
struct S1 {
    T a;
    int b;
};

template <typename T>
void caller(S1<T> *, void (*)(std::type_identity_t<T> *)) {}

int main()
{
    S1<int> s1;
    caller(&s1, [](int *){});
}

这里type_identity_t需要停止从第二个参数推导出t,这会使编译器通过lambda时混淆。

I want to pass an S1 pointer s1 and cb to caller such that the type of cb is inferred from the type of s1->a

Like this?

#include <iostream>
#include <type_traits>

template <typename T>
struct S1 {
    T a;
    int b;
};

template <typename T>
void caller(S1<T> *, void (*)(std::type_identity_t<T> *)) {}

int main()
{
    S1<int> s1;
    caller(&s1, [](int *){});
}

Here type_identity_t is needed to stop T from being deduced from the second argument, which confuses the compiler when passing a lambda.

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