detecting typedef at compile time (template metaprogramming)

后端 未结 4 1231
悲&欢浪女
悲&欢浪女 2021-02-03 10:06

I am currently doing some template metaprogramming. In my case I can handle any \"iteratable\" type, i.e. any type for which a typedef foo const_iterator exists in

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-03 10:48

    Here's another version of a member type trait check:

    template
    struct has_const_iterator
    {
    private:
        typedef char                      yes;
        typedef struct { char array[2]; } no;
    
        template static yes test(typename C::const_iterator*);
        template static no  test(...);
    public:
        static const bool value = sizeof(test(0)) == sizeof(yes);
    };
    

提交回复
热议问题