视频怎么去重审核,2026年视频去重工作流,5款选型指南
2026/7/15 1:25:56
博主介绍:程序喵大人
最近发现了一个有意思的特性:void_t。void_t是C++17引入的一个新特性,它的定义很简单(有些编译器的实现可能不是这样,但也大体类似):
template<class...>usingvoid_t=void;看着它很简单,但它搭配SFINAE却可以在模板元编程中发挥巨大作用。
比如在编译期判断类是否有某个类型using:
template<class,class=std::void_t<>>structhas_type:std::false_type{};template<classT>structhas_type<T,std::void_t<typenameT::type>>:std::true_type{};比如判断是否有某个成员:
template<class,class=std::void_t<>>structhas_a_member:std::false_type{};template<classT>structhas_a_member<T,std::void_t<decltype(std::declval<T>().a)>>:std::true_type{};比如判断某个类是否可迭代:
template<typename,typename=void>constexprboolis_iterable{};template<typenameT>constexprboolis_iterable<T,std::void_t<decltype(std::declval<T>().begin()),decltype(std::declval<T>().end())>>=true;比如判断某个类是否有某个函数:
template<classT,class=void>structhas_hello_func:std::false_type{};template<classT>structhas_hello_func<T,std::void_t<decltype(std::declval<T>().hello())>>:std::true_type{};测试结果:
structHasType{typedefinttype;};structNHasType{inthello;};structHasa{inta;};structNHasa{intb;};structHasHello{voidhello();};structNoHasHello{};intmain(){std::cout<<has_type<HasType>::value<<'\n';// 1std::cout<<has_type<NHasType>::value<<'\n';// 0std::cout<<has_a_member<Hasa>::value<<'\n';// 1std::cout<<has_a_member<NHasa>::value<<'\n';// 0std::cout<<has_hello_func<HasHello>::value<<'\n';// 1std::cout<<has_hello_func<NoHasHello>::value<<'\n';// 0std::cout<<is_iterable<std::vector<double>><<'\n';// 1std::cout<<is_iterable<double><<'\n';// 0}它的原理其实就是利用SFINAE和模板优先找特化去匹配的特性,估计大家应该看示例代码就能明白。
码字不易,欢迎大家点赞,关注,评论,谢谢!