With the current version of C++ (C++03), there really isn't a good way to do this. You could try something like
#define StringHashMap(type) stdext::hash_map<std::string, type, CStringHasher>
But then you run into trouble if you try to specify a template type with a comma in it, like this:
StringHashMap(pair<int, string>) myMap; // Error!
This fails because the preprocessor will tokenize this as
StringHashMap((pair<int), (string>)) myMap; // Error!
which isn't what you want. If you're not going to do anything like this, though, you should be fine. You could alternatively use typedef
s to get around it:
typedef pair<int, int> IntPair;
StringHashMap(IntPair) myMap; // Okay
If you're allowing use of C++0x features, you can use a template using declaration, like this one here:
template <typename T>
using StringHashMap = stdext::hash_map<std::string, T, CStringHasher>;
Unfortunately, C++03 doesn't have a "template typedef" feature like this one, so you'd have to use a newer compiler and won't have as good a portability guarantee.
Hey, wait a minute! My name is templatetypedef
and this is the first time on Stack Overflow that I've ever written that phrase in an answer! You just made my night. :-)