模板模板别名为嵌套模板? [英] template template alias to a nested template?

查看:131
本文介绍了模板模板别名为嵌套模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模板别名在将typename F <T>::type等类型简化为F <T>时非常方便,其中Ttype是类型.

Template aliases are very convenient in simplifying types like typename F <T>::type to just F <T>, where T and type are types.

我想对像F <T>::map这样的模板做同样的事情,即将它们简化为F <T>,其中Tmap是模板结构或别名.

I would like to do the same for templates like F <T>::map, i.e., simplify them to F <T>, where T and map are template structs or aliases.

例如,考虑以下定义:

template <bool B>
using expr = std::integral_constant <bool, B>;

template <bool B>
using _not = expr <!B>;

template <template <typename> class F>
struct neg_f
{
    template <typename T>
    using map = _not <F <T>{}>;
};

template <typename T>
pred = expr < /* ... T ... */ >;  // e.g., pred = expr <true>;

template <template <typename> class F>
struct fun;

现在可以进行以下工作:

Now the following works:

fun <neg_f <pred>::map>

这会方便得多,但失败了:

This would be much more convenient, but it fails:

template <template <typename> class F>
using neg = neg_f <F>::map;

fun <neg <pred> >

(即使map被定义为结构,它也会在neg = neg_f <F>::template map下失败).似乎上述neg的定义更像是模板模板别名"

(It also fails with neg = neg_f <F>::template map, even if map is defined as a struct). It appears that the definition of neg above would rather have to be like a "template template alias"

template <template <typename> class F>
template <typename T>
using neg = neg_f <F>::template map <T>;

但显然没有这样的东西.

but apparently there is no such thing.

那么,有什么解决办法还是我应该留在neg_f <pred>::map上?

So, is there any solution or should I stay with neg_f <pred>::map ?

推荐答案

首先考虑使用typename关键字声明这是一个嵌套类型,而不管类型是什么(例如struct,class等),模板类型,typedef或别名.

At first consider using typename keyword to state that this is a nested type no matter if one is a type (e.g. struct, class, etc), template type, typedef or alias.

别名规范要求您使用 type-id 以指定先前定义的类型.在这种特殊情况下,正确使用 type-id 看起来像这样:

Alias specification requires you to use a type-id to specify a previously defined type. In this particular case correct using of type-id will look like this:

template< template<typename> class F, class T>
using neg_v2 = typename neg_f<F>::template map<T>;

// or

struct foo {};
template< template<typename> class F>
using neg_v1 = typename neg_f<F>::template map<foo>;

您最初尝试做的是将 template-name neg_f<F>::map用作 type-id .这是不正确的.

What you try to do originally is to use template-name neg_f<F>::mapas a type-id. This is not correct.

可能您想以某种方式从F推断出T参数,以便在template map<T>处使用该参数,但这不适用于您无法解析T的最终用例fun<neg<pred>>.

Probably you want to deduce somehow the T parameter from F to be used at template map<T> but this is not applicable to your final use-case fun<neg<pred>> where T is not resolved.

这篇关于模板模板别名为嵌套模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆