28

我想创建一个将 std::vector 引用作为参数的类方法,并且我想将它与不同类型的数据一起使用。

该函数应如下所示:

void some_function(const std::vector & vect){ //do something with vector }

我想用它来举例:

std::vector<int> v1;
some_function(v1);
std::vector<string> v2;
some_function(v2);

我希望我的观点很清楚。我是否必须制作这样的模板方法:

template<class T>
void some_function(std::vector<T> & vect){}

或者我可以用其他方式吗?如果必须,请告诉我如何在课堂上编写该方法。

感谢帮助!

4

1 回答 1

42

template函数接受 any std::vectorby的正确方法const&是:

template<typename T, typename A>
void some_func( std::vector<T,A> const& vec ) {
}

第二个参数是“分配器”,在某些高级用法中std::vector它不是默认的。如果您只接受std::vector<T>,您some_func将拒绝std::vectors 与替代分配器。

现在,还有其他方法可以解决这个问题,我将快速列出。我将按成本效益比递减的方式列出它们——上面的一个可能是你想要的,下一个有时是有用的,然后我将分支到很少值得考虑的过度设计的案例(但可能有用在某些极端情况下)。

T您可以通过then 测试来接受任意类型T&&以确定是否typename std::remove_reference<T>::type是一种std::vector. 这将允许您对传入的std::vector. 它还可以让您更改用于测试的谓词,使其不仅仅接受std::vector: 在大多数情况下,const&可能std::vector只需要一些任意的随机访问容器。

A ridiculously fancy way would be to do a two-step function. The second step takes a type-erased random-access range view (or just a range-view if you don't need random access) for a fixed type T with SFINAE to ensure that the incoming object is compatible, the first step deduces the container type of the passed in type and calls the second step in a SFINAE context (auto some_func(...)->decltype(...)).

As type erasure of std::vector<T> const& to a random-access range view of contiguous Ts doesn't lose much functionality, an advantage would be that you could guarantee that the body of your function is exactly the same for std::vector<T> const& and for T[n] and for std::array<T,n>.

It isn't a big advantage, especially for the boilerplate required.

may make this much easier, because the multi-step SFINAE above will collapse into a few requires clauses.

于 2013-09-30T13:05:20.673 回答