很多时候,我想为不同的STL容器专门设计一个函数。然而,我并不想逐个进行专门设计,因为其中一些共享大部分所需的接口,例如std::vector和std::deque。
在我的使用情况中,主要有三种类别(类似于向量、集合和映射)。 例如,我想实现类似以下的东西,
template <class T>
struct A {
template <class Y, class... Z>
void func( Y y , Z... z ){
//hypothetical static_if
static_if ( T is similar to vector, deque, or boost::stable_vector etc which have push_back ) {
t.push_back(y);
}
else static_if ( T is similar to set, unordered_set or boost::flat_set etc which have emplace) {
t.emplace(y);
}
else static_if ( T is similar to map, unordered_map or boost::flat_map etc which has emplace) {
t.emplace(y, z...);
}
}
T t;
};
我认为这似乎是不可能的,但我希望能有某种方法来解决这个问题。如果可以扩展到列表类型(std::list、std::forward_list等)或boost::heap等,那就更好了。但是实现这个目标似乎太难了。