我想制作一个isIn
需要std::span
.
这是我的尝试:
#include <span>
template <typename T1, typename T2>
bool isIn(const T1& x, std::span<const T2> v)
{
for (const T2& e : v)
if (e == x)
return true;
return false;
}
// this one would work, but I want my function to be generic
/*bool isIn(int x, std::span<const int> v)
{
for (int e : v)
if (e == x)
return true;
return false;
}*/
int main()
{
const int v[] = {1, 2, 3, 4};
isIn(2, v); // I want this, but doesn't compile
//isIn(2, std::span<const int>(v)); // this works fine
}
正如你所看到的,我可以通过做这个演员来解决:
isIn(2, std::span<const int>(v));
但这很冗长,我想做这样的事情:
isIn(2, v);
有什么方法可以实现吗?