在C ++中推导模板类型的类型 [英] Deduce type of template type in C++
本文介绍了在C ++中推导模板类型的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当为iterator范围编写通用函数时,我通常这样做:
When writing generic functions for "iterator" ranges I usually do:
template <typename Iter> auto func(Iter &first, Iter &last)
{
using IterType = typename std::decay<decltype(*first)>::type;
...
}
另一种方法似乎是:
template <typename Iter> auto func(Iter &first, Iter &last)
{
using IterType = typename std::iterator_traits<Iter>::value_type;
...
}
还有第三个:
template <typename Iter> auto func(Iter &first, Iter &last)
{
using IterType = typename Iter::value_type;
...
}
不应用 iterator_traits
Without applying iterator_traits
.
理论上,我的函数应该只接收迭代器第一
和 last
,第二种形式理想情况下(imho)最常用的方法来获取类型。但是使用 typename std :: decay< decltype(* first)> :: type
最通用的成语,以便不对 $
In theory, my functions should only receive iterators as first
and last
and the second form would ideally (imho) the most idiomatic way to get the type. But is using typename std::decay<decltype(*first)>::type
the most generic idiom in order to not impose restrictions to Iter
like having a value_type
defined?
推荐答案
- 第一个不适用于代理(std :: vector< bool>)
-
- 第三个不适用于指针。
这篇关于在C ++中推导模板类型的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文