使用std::enable_if_t
来帮助选择匹配的函数。以容器是否含有reserve
方法作为示例。
1. 实现方式一
cpp
#include <iostream>
#include <vector>
#include <list>
#include <type_traits> // std::declval
/* 判断传入类型是否有reserve方法 */
template<typename ...>
using void_t = void;
template <typename T, typename = void>
struct has_reserve: std::false_type {};
template <typename T>
struct has_reserve<T, void_t<decltype(std::declval<T&>().reserve(1U))>> : std::true_type { };
template<typename C, typename T>
std::enable_if_t<has_reserve<C>::value, void>putData(C& container, T* ptr, size_t size) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
container.reserve(container.size() + size);
for(size_t i = 0; i < size; ++i){
container.push_back(ptr[i]);
}
}
template<typename C, typename T>
std::enable_if_t<!has_reserve<C>::value, void>putData(C& container, T* ptr, size_t size) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
for(size_t i = 0; i < size; ++i){
container.push_back(ptr[i]);
}
}
int main() {
std::list<int> lst;
std::vector<int> vec;
int a[] = {1,2,3,4,5};
putData(lst, a, 5); // 调用没有reserve函数版本的putData
putData(vec, a, 5); // 调用含有reserve函数版本的putData
for(const auto&it: lst){
std::cout << it << " ";
}
std::cout << std::endl;
for(const auto&it: vec){
std::cout << it << " ";
}
std::cout << std::endl;
}
输出:
bash
std::enable_if_t<(! has_reserve<C>::value), void> putData(C&, T*, size_t) [with C = std::__cxx11::list<int>; T = int; std::enable_if_t<(! has_reserve<C>::value), void> = void; size_t = long unsigned int]
std::enable_if_t<has_reserve<C>::value, void> putData(C&, T*, size_t) [with C = std::vector<int>; T = int; std::enable_if_t<has_reserve<C>::value, void> = void; size_t = long unsigned int]
1 2 3 4 5
1 2 3 4 5
2. 实现方式二
cpp
#include <iostream>
#include <vector>
#include <list>
#include <type_traits> // std::declval
#include <string>
/* 判断传入类型是否有reserve方法 */
template<typename ...>
using void_t = void;
template <typename T, typename = void>
struct has_reserve: std::false_type {};
template <typename T>
struct has_reserve<T, void_t<decltype(std::declval<T&>().reserve(1U))>> : std::true_type {}; // 针对容器中存储的是整型数据
//struct has_reserve<T, void_t<decltype(std::declval<T&>().reserve(std::string("abc")))>> : std::true_type {}; // 针对容器中存储的是std::string
template<typename C, typename T>
void _putData(C& container, T* ptr, size_t size, std::true_type) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
container.reserve(container.size() + size);
for(size_t i = 0; i < size; ++i){
container.push_back(ptr[i]);
}
}
template<typename C, typename T>
void _putData(C& container, T* ptr, size_t size, std::false_type) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
for(size_t i = 0; i < size; ++i){
container.push_back(ptr[i]);
}
}
template<typename C, typename T>
void putData(C& container, T* ptr, size_t size){
_putData(container, ptr, size, has_reserve<C>{});
}
int main() {
std::list<int> lst;
std::vector<int> vec;
int a[] = {1,2,3,4,5};
putData(lst, a, 5); // 调用没有reserve函数版本的putData
putData(vec, a, 5); // 调用含有reserve函数版本的putData
for(const auto&it: lst){
std::cout << it << " ";
}
std::cout << std::endl;
for(const auto&it: vec){
std::cout << it << " ";
}
std::cout << std::endl;
}
输出结果:
bash
void _putData(C&, T*, size_t, std::false_type) [with C = std::__cxx11::list<int>; T = int; size_t = long unsigned int; std::false_type = std::integral_constant<bool, false>]
void _putData(C&, T*, size_t, std::true_type) [with C = std::vector<int>; T = int; size_t = long unsigned int; std::true_type = std::integral_constant<bool, true>]
1 2 3 4 5
1 2 3 4 5