templated typedef?

前端 未结 4 1501
我寻月下人不归
我寻月下人不归 2020-12-04 19:10

I\'m using libgc, a garbage collector for C and C++. To make STL containers garbage collectible one must use the gc_allocator.

Instead of writing

s         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 19:58

    You cannot use a "templated typedef", but you can use a convenience class/struct with an inner type:

    template
    struct TypeHelper{
        typedef std::vector > Vector;
    };
    

    and then use in your code

    TypeHelper::Vector v;
    TypeHelper::Vector::iterator it;
    

    And something similar for the map:

    template
    struct MapHelper{
        typedef std::map > Map;
    };
    

    EDIT - @Vijay: I don't know if there's another possible workaround, that's how I would do it; a macro might give you a more compact notation, but personally I wouldn't like it:

    #define GCVECTOR(T) std::vector >
    

    EDIT - @chmike: Please note that the TypeHelper solution does not require you to redefine constructors!

提交回复
热议问题