我正在实现一个函数,它希望循环一个std::数组中的许多元素,但我并不关心std::array有多长时间。所以我想到了以下功能:
#include <stdio.h>
#include <array>
#include <iterator>
void foo(std::array<bool,0>::const_iterator begin, std::array<bool,0>::const_iterator end)
{
printf("iterator");
}
int main()
{
std::array<bool, 25> one;
std::array<bool, 33> two;
foo(one.cbegin(), one.cend());
foo(two.cbegin(), two.cend());
}我对此非常满意,除了std::array<bool,0>。我的问题是,是否有另一种方法来指定此函数所需的迭代器?
更新
有些事我应该提一下。当然,这段代码是更大范围的一部分,我试图尽可能地隐藏更多的细节。
bool的。class MyInterface
{
public:
virtual foo(std::array<bool,0>::const_iterator begin, std::array<bool,0>::const_iterator end) = 0;
~MyInterface() = default;
};我记得虚拟函数不能被模板化。这意味着我将不得不模板我的整个界面,这将完全失去了为什么我要尝试这一点。
发布于 2020-05-04 12:34:59
只需使用span
#include <array>
#include <span>
class MyInterface {
public:
virtual void foo(std::span<bool> barr) = 0;
// interface destructors should be virtual
virtual ~MyInterface() = default;
};
void bar(MyInterface& interface) {
std::array<bool, 42> arr;
interface.foo(arr);
}https://stackoverflow.com/questions/61592200
复制相似问题