一种值类型的容器迭代器模板 [英] Container Iterator Template for one value type
问题描述
您之前曾帮助过我,所以在这里我再次遇到另一个问题,希望能得到答案.
you helped me before so here I am again with another question in hope for an answer.
我有一个函数,可以处理一系列 std :: complex< float>
值.我最初对函数定义的尝试是
I have a function that processes a range of std::complex<float>
values. My initial attempt of a function definition was
// Example 1
using namespace std; // not in original code
Errc const& process(vector<complex<float>>::iterator begin, vector<complex<float>>::iterator end);
但这仅适用于向量,不适用于C样式的数组,std :: arrays,范围等.
This however only works for vectors, not C-style arrays, std::arrays, ranges, etc.
经过一番摆弄,大量的Google和一点点运气之后,我设法构建了这一点:
After some fiddling, lots of Google and a little bit of luck, I managed to construct this:
// Example 2
using namespace std; // not in original code
template<
template<typename T> class C,
typename T,
typename C<T>::iterator iterator
>
Errc const& process(typename C<T>::iterator begin, typename C<T>::iterator end);
我什至不确定那是否可行,但至少可以编译.我认为它的作用是定义一个函数,该函数处理具有任意值类型的任何容器.这将是一个问题,因为我只能处理std :: complex(或类似的复杂浮点值),而不能处理例如std :: string.
I am not even sure if that works but it compiles at least. What I think it does, is define a function that processes any container with an arbitrary value type. This would be a problem, as I can only process std::complex (or maybe similar complex floating point values) but not std::string for example.
我想做什么:
// Example 3
using namespace std; // not in original code
template<
template<typename complex<float>> class C,
typename C<complex<float>>::iterator iterator
>
Errc const& process(typename C<complex<float>>::iterator begin, typename C<complex<float>>::iterator end);
但这显然不是实现此目的的方法.我大约60%的人肯定我在示例2中也弄乱了.
But this apparently is not the way to do this. I am about 60% percent sure I also messed something up in example 2.
任何帮助或提示,我们将不胜感激.谢谢
Any help or hints are greatly appreciated. Thank you
推荐答案
您的示例2几乎是正确的
Your example 2 is nearly correct
template<
template<typename T> class C,
typename T,
typename C<T>::iterator iterator // That is wrong
>
Errc const& process(typename C<T>::iterator begin, typename C<T>::iterator end);
应为:
template<template <typename> class C, typename T>
Errc const& process(typename C<T>::iterator begin, typename C<T>::iterator end);
但是问题在于 C
/ T
不可推论,您必须这样称呼它:
But issue is that C
/T
are not deducible, and you have to call it like:
process<std::vector, std::complex<float>>(v.begin(), v.end());
C-array
和 std :: array
与 template< typename>不匹配C类
都没有(并且 std :: vector
也具有默认分配器:-/)
And C-array
and std::array
doesn't match template <typename> class C
neither (and std::vector
has default allocator too :-/)
更简单
template<typename Iterator>
Errc const& process(Iterator begin, Iterator end);
可能带有一些SFINAE
possibly with some SFINAE
template <typename Iterator,
std::enable_if_t<std::is_same_v<std::complex<float>,
std::iterator_traits<Iterator>::value_type>, int> = 0>
Errc const& process(Iterator begin, Iterator end);
或C ++ 20要求:
or C++20 requires:
template <typename Iterator>
Errc const& process(Iterator begin, Iterator end)
requires (std::is_same_v<std::complex<float>, std::iterator_traits<Iterator>::value_type);
如果仅需要连续序列,则可以使用 std :: span
If you want only contiguous sequences, you might use std::span
Errc const& process(std::span<std::complex<float>)
这篇关于一种值类型的容器迭代器模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!