且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查模板参数是否为迭代器类型?

更新时间:2022-01-25 19:22:59

template<class T>
struct is_iterator
{   
    static T makeT();
    typedef void * twoptrs[2];  // sizeof(twoptrs) > sizeof(void *)
    static twoptrs & test(...); // Common case
    template<class R> static typename R::iterator_category * test(R); // Iterator
    template<class R> static void * test(R *); // Pointer

    static const bool value = sizeof(test(makeT())) == sizeof(void *); 
};