等同于`模板`的`using`别名 [英] Equivalent of `using` aliases for `template`s

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

问题描述

C ++ 11添加了别名模板,例如:

C++11 added alias templates such as:

 template<typename T> using identity = T;
 template<bool b, typename T = void> using EnableIf = typename std::enable_if<b, T>::type;

这些比旧的模板更容易使用类型映射,在 :: type 字段中为您提供返回值,因为即使您的类型参数依赖于本地上下文,也不需要通知编译器的结果是一个类型。

These are much easier to use than the old template type maps that give you the return value in a ::type field because even if your type arguments are dependent on the local context, you don't need to inform the compiler that the result is a type.

实际上,你提起 typename 从使用位置到使用别名。

In effect, you hoist the typename from the location of use to the using alias.

有什么等同物可以用来摆脱生成的外来 template s?

Is there anything equivalent that can be used to get rid of produced extraneous templates?

假设你有一个元函数,其输出是类或别名模板而不是类型。当前方法是这样的:

Suppose you had a metafunction whose output was a class or alias template instead of a type. The current method is something like this:

template<typename T>
struct my_meta {
  template<typename U>
  using Template = identity<U>;
};

template<typename T>
struct my_meta {
  template<typename U>
  using Template = int;
};

我们可以这样使用:

template<typename T, typename U>
typename my_meta<T>::template Template<U>
do_stuff( U&& ) { return {}; }

返回中的额外模板关键字类型存在以消除我的元函数的返回值是我想要消除的。

That extra template keyword in the return type exists to disambiguate the return value of my meta function is what I want to eliminate.

有任何方式向编译器指示元计算的结果是另一个在调用位置不使用模板关键字的C ++ 11或C ++ 1y中的别名或类模板?

Is there any way to indicate to the compiler that the result of a metacomputation is another alias or class template in C++11 or C++1y, without using the template keyword at the location of invocation?

Ie:

template<typename T, typename U>
my_meta_template<T><U>
do_stuff( U&& ) { return {}; }

或甚至

template<template<typename> class Template>
void do_more_stuff() {}

template<typename T>
void do_stuff() {
  // syntax I want: just produce an alias or class template directly:
  do_more_stuff< my_meta_template<T> >();
  // vs what I find is required: the disambiguator:
  do_more_stuff< my_meta<T>::template Template >();
};


推荐答案

> template 是简单的warper,它将为你做:

Best way I know to remove template is made simple warper that will do it for you:

template<typename Meta, typename U>
using apply = typename Meta::template Template<U>;

以及您以前使用的每个位置 template< typename>类模板替换为 typename Meta

and every where you previously used template<typename> class Template replace it with typename Meta.

template<typename Meta>
void do_more_stuff()
{
    apply<Meta, int>::func(); //=== Meta::template Template<int>::func();
}

template<typename T>
void do_stuff() {
  do_more_stuff< my_meta<T> >();
  do_more_stuff< my_meta<T> >();
};

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

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