template template alias to a nested template?

我的梦境 提交于 2020-01-13 08:11:06

问题


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

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.

For instance, consider the following definitions:

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> >

(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.

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


回答1:


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.

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>;

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

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.



来源:https://stackoverflow.com/questions/18699973/template-template-alias-to-a-nested-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!