美文网首页
【cxx-prettyprint源码学习】is_containe

【cxx-prettyprint源码学习】is_containe

作者: 长不胖的Garfield | 来源:发表于2016-12-22 11:36 被阅读0次

    is_container的作用

    cxx-prettyprint是提供给C++容器使用的,需要正确判定是否为C++容器,从而避免其它影响。

    如何判断

    鉴于目标是打印容器内容,就需要迭代器来遍历容器内容,也就是说,如果类型是容器,需要满足三个条件:

    • const_iterator
    • begin
    • end

    遍历时begin和end界定了容器元素范围,其类型都是iterator,为避免对容器元素产生影响,应当使用const_iterator根据[begin,end)进行遍历并输出内容到ostream。

    何为SFINAE

    Substitution Failure Is Not An Error

    我的理解是:当匹配函数模板失败时,该函数模板只是被丢弃而不是触发编译错误。

    看以下示例:

    #include <iostream>
     
    //函数模板1
    void test(...)
    {
        std::cout << "Catch-all overload called\n";
    }
     
    //函数模板2
    template<class C, class F>
    auto test(C c, F f) -> decltype((void)(c.*f)(), void())
    {
        std::cout << "Reference overload called\n";
    }
     
    //函数模板3
    template<class C, class F>
    auto test(C c, F f) -> decltype((void)((c->*f)()), void())
    {
        std::cout << "Pointer overload called\n";
    }
     
    struct X { void f() {} };
     
    int main(){
      X x;
      test( x, &X::f); 
      test(&x, &X::f);
      test(42, 1337);
    }
    

    其中函数模板1能够适用于所有类型,函数模板2适用于类实例引用及其成员函数,函数模板3适用于类指针及其成员函数:

    • test( x, &X::f)
      这个不能匹配到函数模板3,但是能够匹配函数模板2和函数模板1,那最终使用的就是函数模板2
    • test(&x, &X::f)
      这个不能匹配到函数模板2,但是能够匹配函数模板3和函数模板1,那最终使用的就是函数模板3
    • test(42, 1337)
      这个只能匹配到函数模板1

    在这个过程中,匹配不到的情况下不会引起编译错误,只是根据可以匹配的模板继续匹配。

    判定是否有const_iterator

    判断是否有类型T是否有const_iterator利用了SFINAE:

    struct sfinae_base
    {
        using yes = char;
        using no = yes[2];
    };
     
    template<typename T>
    struct has_const_iterator :private sfinae_base
    {
    private:
        template <typename C> static yes & test(typename C::const_iterator*);
        template <typename C> static no  & test(...);
    public:
        static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
        using type = T;
    };
    

    当类型T有const_iterator类型时,test<T>(nullptr)会匹配到static yes & test(typename C::const_iterator*),这是返回的类型是yes,否则返回的类型是no

    这样,通过has_const_iterator<T>::value即可从编译期判断是否有const_iterator了。

    判断是否有begin和end

    template <typename T>
    struct has_begin_end :private sfinae_base
    {
    private:
        template<typename C>
        static yes & f(template std::enable_if<std::is_same<decltype(static_cast<typename C::const_iterator(C::*)() const>(&C::begin)),
            typename C::const_iterator(C::*)() const>::value>::type *);
     
        template<typename C> static no & f(...);
     
        template<typename C>
        static yes & g(template std::enable_if<std::is_same<decltype(static_cast<typename C::const_iterator(C::*)() const>(&C::end)),
            typename C::const_iterator(C::*)() const>::value>::type *);
     
        template<typename C> static no & g(...);
    public:
        static bool const beg_value = sizeof(f<T>(nullptr)) == sizeof(yes);
        static bool const end_value = sizeof(g<T>(nullptr)) == sizeof(yes);
    };
    

    可以看到基本的实现方式是一致的,之所以比较复杂是为了判断有没有返回const_iterator的begin/end方法。

    static_cast

    静态cast,只能在能够转换的情况下可以进行转换,也就是说,如果&C::begin即使定义的有,如果转换不成功,则会匹配到template<typename C> static no & f(...)

    decltype

    用来推断类型

    decltype(static_cast<typename C::const_iterator(C::*)() const>(&C::begin))
    

    得到的就是C::const_iterator

    is_same

    判定两个类型是否一致,而且const-volatile也要一致。

    enable_if

    用来根据条件为SFINAE移除不匹配的函数模板

    判断是否为container

    结合上述三种条件,是否为container的判定方法如下

    template <typename T>
    struct is_container:public std::integral_constant<bool,
         detail::has_const_iterator<T>::value &&
         detail::has_begin_end<T>::beg_value &&  
         detail::has_begin_end<T>::end_value> {};
    

    这样is_container<T>::value即可返回true_type或者false_typeenable_if来进一步控制是否应用函数模板到类型T了。

    对其它非常规类型的支持

    明白了判断机制,那么想增加其它非常规类型的支持,也就可以处理了:

    数组

    template <typename T, std::size_t N>
    struct is_container<T[N]> : std::true_type { };
    

    valarray

    template <typename T>
        struct is_container<std::valarray<T>> : std::true_type { };
    

    tuple

    template <typename ...Args>
        struct is_container<std::tuple<Args...>> : std::true_type { };
    

    学到的知识

    • SFINAE
    • decltype
    • is_same
    • enable_if

    相关文章

      网友评论

          本文标题:【cxx-prettyprint源码学习】is_containe

          本文链接:https://www.haomeiwen.com/subject/sabcvttx.html