什么是模板推导指南,何时应使用它们? [英] What are template deduction guides and when should we use them?

查看:124
本文介绍了什么是模板推导指南,何时应使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 17标准引入了模板推导指南。我认为它们与该版本标准中引入的针对构造函数的新模板自变量推论有关,但是我还没有看到关于它们的含义和用途的简单的,常见问题解答风格的解释。 / p>


  • 什么是C ++ 17中的模板推导指南?


  • 为什么(何时)需要它们?


  • 我如何声明它们?



解决方案

模板推导指南是与模板类相关联的模式,它们告诉编译器如何翻译一组参数(及其类型)。



最简单的示例是 std :: vector 及其采用迭代器对的构造函数

  template< typename Iterator>。 
void func(Iterator first,Iterator last)
{
vector v(first,last);
}

编译器需要弄清楚什么 vector< T> ; T 类型。我们知道答案是什么; T 应该为 typename std :: iterator_traits< Iterator> :: value_type 。但是我们如何告诉编译器无需必须键入 vector< typename std :: iterator_traits< Iterator> :: value_type>



您使用推论指南:

  template< typename Iterator>向量(迭代器b,迭代器e)-> 
vector< typename std :: iterator_traits< Iterator> :: value_type> ;;

这告诉编译器,当您调用 vector 匹配该模式的构造函数,它将使用-> 右边的代码推导 vector 专业化



当从参数中推导类型不基于那些参数之一的类型时,您需要使用指南。从 initializer_list 初始化 vector 显式使用 vector ' s T ,因此不需要参考。



左侧不一定指定构造函数。它的工作方式是,如果在类型上使用模板构造函数推导,则它将与针对所有推导指南传递的参数匹配(主模板的实际构造函数提供隐式指南)。如果存在匹配项,它将使用它来确定要提供给该类型的模板参数。



但是一旦推论完成,编译器便会确定模板参数对于类型,该类型的对象的初始化就好像没有发生一样进行。也就是说,所选的推导指南不必与所选的构造函数相匹配。



这也意味着您可以将指南与汇总和汇总一起使用初始化:

  template< typename T> 
结构Thingy
{
T t;
};

Thingy(const char *)-> Thingy< std :: string> ;;

Thingy事物{ A String}; //thing.t是`std :: string`。

因此,推导仅用于确定要初始化的类型。一旦确定,初始化的实际过程将像以前一样工作。


The C++17 standard introduces "template deduction guides". I gather they're something to do with the new template argument deduction for constructors introduced in this version of the standard, but I haven't yet seen a simple, FAQ-style explanation of what they are and what they're for.

  • What are template deduction guides in C++17?

  • Why (and when) do we need them?

  • How do I declare them?

解决方案

Template deduction guides are patterns associated with a template class that tell the compiler how to translate a set of parameter (and their types) into template arguments.

The simplest example is that of std::vector and its constructor that takes an iterator pair.

template<typename Iterator>
void func(Iterator first, Iterator last)
{
  vector v(first, last);
}

The compiler needs to figure out what vector<T>'s T type will be. We know what the answer is; T should be typename std::iterator_traits<Iterator>::value_type. But how do we tell the compiler without having to type vector<typename std::iterator_traits<Iterator>::value_type>?

You use a deduction guide:

template<typename Iterator> vector(Iterator b, Iterator e) -> 
    vector<typename std::iterator_traits<Iterator>::value_type>;

This tells the compiler that, when you call a vector constructor matching that pattern, it will deduce the vector specialization using the code on the right of ->.

You need guides when the deduction of the type from the arguments is not based on the type of one of those arguments. Initializing a vector from an initializer_list explicitly uses the vector's T, so it doesn't need a guide.

The left side doesn't necessarily specify a constructor. The way it works is that, if you use template constructor deduction on a type, it matches the arguments you pass against all deduction guides (actual constructors of the primary template provide implicit guides). If there is a match, it uses that to determine which template arguments to provide to the type.

But once that deduction is done, once the compiler figures out the template parameters for the type, initialization for the object of that type proceeds as if none of that happened. That is, the deduction guide selected does not have to match the constructor selected.

This also means that you can use guides with aggregates and aggregate initialization:

template<typename T>
struct Thingy
{
  T t;
};

Thingy(const char *) -> Thingy<std::string>;

Thingy thing{"A String"}; //thing.t is a `std::string`.

So deduction guides are only used to figure out the type being initialized. The actual process of initialization works exactly as it did before, once that determination has been made.

这篇关于什么是模板推导指南,何时应使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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