template template alias to a nested template?
Asked Answered
C

1

10

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 ?

Christiansand answered 9/9, 2013 at 13:59 Comment(8)
I asked a similar question #17356987 and there doesn't seem to be a good answer.Fillbert
Indeed, the last part of your question is very similar or the same. Thanks.Christiansand
Your using of spaces before < makes everything extremely unpleasant to read, for some reasons.Danuloff
@Jeffrey I am sorry for this. On the other hand, I would find template<template<typename> class F> extremely unpleasant; template< template< typename > class F > as well. It's pretty standard using space before parentheses, e.g. if (x < 0), so spaces before < look natural to me.Christiansand
I do not understand what those neg_f and other stuff represent, if you described that somewhat better perhaps we could come up with a solutionChild
Also: I don't see how that can compile with {} (initializing an object in a declaration)Child
@DavidKernin My apologies, neg_f is supposed to negate a type predicate, that is, neg_f<F>::map is another type predicate that, when applied to a type T, gives the complement of whatever F<T> gives. I didn't explain because its definition is so simple and because it's just a example, it's not essential to the problem (I could equally use a foo-like example).Christiansand
@DavidKernin On _not <F <T>{}>: F is a type predicate, i.e. F<T> is std::integral_constant<bool,B> for some B as shown by the definitions. Meaning that F<T>{} is an object of literal type that is implicitly converted to bool, in turn used as non-type template parameter for _not. I hope this helps.Christiansand
L
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.

Larainelarboard answered 11/4, 2014 at 10:5 Comment(4)
So the error list shows the root cause. See my edits to the answer.Larainelarboard
I didn't try to use template-name neg_f<F>::map as a type-id. You did that.Christiansand
here is your try to use a template-name neg_f <F>::map as a type-id ;) template <template <typename> class F> using neg = neg_f <F>::mapLarainelarboard
This was hypothetical code to illustrate the question, if you read the entire sentence. Anyway, I removed the downvote.Christiansand

© 2022 - 2024 — McMap. All rights reserved.