本文主要是介绍判断一个类型是否有reserve方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用std::enable_if_t
来帮助选择匹配的函数。以容器是否含有reserve
方法作为示例。
1. 实现方式一
#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函数版本的putDataputData(vec, a, 5); // 调用含有reserve函数版本的putDatafor(const auto&it: lst){std::cout << it << " ";}std::cout << std::endl;for(const auto&it: vec){std::cout << it << " ";}std::cout << std::endl;
}
输出:
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. 实现方式二
#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::stringtemplate<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函数版本的putDataputData(vec, a, 5); // 调用含有reserve函数版本的putDatafor(const auto&it: lst){std::cout << it << " ";}std::cout << std::endl;for(const auto&it: vec){std::cout << it << " ";}std::cout << std::endl;
}
输出结果:
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
这篇关于判断一个类型是否有reserve方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!