Typedef a template class without specifying the template parameters

前端 未结 2 512
名媛妹妹
名媛妹妹 2020-12-24 02:21

I\'m trying to typedef either an unordered_map or std::map depending whether there are TR1 libraries available. But I don\'t want to specify the template parameters. From wh

2条回答
  •  旧巷少年郎
    2020-12-24 02:57

    The way I've seen this done is to wrap the typedef in a template-struct:

    template
    struct myMap
    {
    #ifdef _TR1
        typedef std::tr1::unordered_map type;
    #else
        typedef std::map type;
    #endif
    };
    

    Then in your code you invoke it like so:

    myMap::type myMapInstance;
    

    It may be a little more verbose than what you want, but I believe it meets the need given the current state of C++.

提交回复
热议问题