如何强制使用类型别名的模板类实例化

发布于 2025-02-12 04:11:15 字数 558 浏览 0 评论 0原文

我有一个模板类,可以使用大量的模板参数。

这是一个仅在预定义类型列表上使用的助手类。 我使用类型的别名(使用或typedef),以便用户不会被模板suff污染。

由于打字员是硬编码的,我想强制T类的实例化,但是由于列表很长,因此我想强制实例化而无需重复所有参数。

我该怎么办?

template <class... X>
class T {
};

// alias the type so that the user don't get mad with type list
using Z = T<int, bool, char, long
            /* possibly a long list of parameters*/ >;


// this works but needs to repeat all template arguments
template class T<int, bool, char, long>;

// how to force instantiation of T using Z?

I have a template class that takes a large number of template parameters.

This is an helper class that is used only on a predefined list of types.
I use a type alias (using or typedef) so that the user does not get polluted with the template suff.

As the typelist is hardcoded, I would like to force the instantiation of the class T, but as the list is quite long I'd like to force instantiation without repeating all parameters.

How could I do that?

template <class... X>
class T {
};

// alias the type so that the user don't get mad with type list
using Z = T<int, bool, char, long
            /* possibly a long list of parameters*/ >;


// this works but needs to repeat all template arguments
template class T<int, bool, char, long>;

// how to force instantiation of T using Z?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

混吃等死 2025-02-19 04:11:15

作为解决方法,我使用了宏...

#define MY_TYPE T<int,bool,char,long>

using Z = MY_TYPE;

template class MY_TYPE;

我仍在寻找更好的解决方案...

As a workaround I used a macro...

#define MY_TYPE T<int,bool,char,long>

using Z = MY_TYPE;

template class MY_TYPE;

I'm still looking for a better solution...

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