简体   繁体   中英

alias for nested template type

I have the following code:

template <template <typename, typename...> typename trait_t, typename arg_t>
struct BindFirst
{
    template <typename... arg_ts>
    using result_t = trait_t<arg_t, arg_ts...>;
};

#define BIND_FIRST(trait_t, arg_t) BindFirst<trait_t, arg_t>::template result_t

you can use it to bind the first argument of a trait like this:

BIND_FIRST(std::is_same, double)

The result is equivalent to:

template <typename T>
struct IsInt : std::is_same<double, T> { };

The difference is, that you can use it inline. For example like this:

using result_t = find_t<type_list, BIND_FIRST(std::is_same, double)>;

This works but i like to avoid the define. I tried to use an alias. But I have no idea how to apply it. Is there any way to replace the define?

You can use templates with using to create an alias template, just like you did for result_t .

template <typename... Args>
using IsDouble = BindFirst<std::is_same, double>::template result_t<Args...>;

You can limit the Args... to a single type T as well, it doesn't have to be variadic.

Edit: If your goal is to reduce boilerplate, you may want to opt for something like this

template <typename T, typename U>
using IsSameAs = std::is_same<T, U>;

template <typename T>
using IsDouble = IsSameAs<double, T>;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM