考虑以下简单的地图:
class MyCoolMap : public unordered_map<const char *, const char *>
{
public:
ProtoTypeMap()
{
insert(value_type("in1", "out1"));
insert(value_type("in2", "out2"));
...
insert(value_type("inN", "outN"));
}
};
Run Code Online (Sandbox Code Playgroud)
现在,假设我需要使这个地图可用于char和wchar_t字符串.所以,我重写如下:
template<class C>
class MyCoolMap : public unordered_map<const C *, const C *>
{
public:
MyCoolMap()
{
insert(value_type("in1", "out1"));
insert(value_type("in2", "out2"));
...
insert(value_type("inN", "outN"));
}
};
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用C=wchar_t.问题是我不知道如何模拟char文字和wchar_t文字之间的差异.现在我看到两个丑陋的解决方案.
解决方案1 - 专注MyCoolMap于wchar_t:
template<>
class MyCoolMap<wchar_t> : public unordered_map<const wchar_t *, const wchar_t *>
{
public:
MyCoolMap()
{
insert(value_type(L"in1", L"out1"));
insert(value_type(L"in2", L"out2"));
...
insert(value_type(L"inN", L"outN"));
}
};
Run Code Online (Sandbox Code Playgroud)
这很糟糕,因为整个逻辑是重复的.
解决方案2 - 解决方案的特点:
#define _TOWSTRING(x) L##x
#define TOWSTRING(x) _TOWSTRING(x)
template <class C, int> struct special_string;
#define DECL_SPECIAL_STRING(STR) \
const int ss_##STR = __LINE__; \
template<> struct special_string<char, ss_##STR> { static const char *get_value() { return #STR; } }; \
template<> struct special_string<wchar_t, ss_##STR> { static const wchar_t *get_value() { return TOWSTRING(#STR); } };
DECL_SPECIAL_STRING(in1)
DECL_SPECIAL_STRING(out1)
DECL_SPECIAL_STRING(in2)
DECL_SPECIAL_STRING(out2)
...
DECL_SPECIAL_STRING(inN)
DECL_SPECIAL_STRING(outN)
template<class C>
class MyCoolMap : public unordered_map<const C *, const C *>
{
public:
MyCoolMap()
{
#define INSERT_MAPPING(in, out) insert(value_type(special_string<C, ss_##in>::get_value(), special_string<C, ss_##out>::get_value()))
INSERT_MAPPING(in1, out1);
INSERT_MAPPING(in2, out2);
...
INSERT_MAPPING(inN, outN);
#undef INSERT_MAPPING
}
};
Run Code Online (Sandbox Code Playgroud)
这样我就不需要复制逻辑了,但是这很冗长并且很大程度上依赖于宏.
肯定有更好的办法; 我只是没有看到它.
我正在使用VS2010.
编辑
我很高兴提出了一个更简单的解决方案 - 学分转到/sf/users/419121/.我不得不做一些小修改来使它编译,但是:
#define _TOWSTRING(x) L##x
#define TOWSTRING(x) _TOWSTRING(x)
template<typename C> const C * ChooseCW(const char * c, const wchar_t * w);
template<> const char * ChooseCW<char>(const char * c, const wchar_t * w)
{
return c;
}
template<> const wchar_t *ChooseCW<wchar_t>(const char * c, const wchar_t * w)
{
return w;
}
#define CW(C, STR) ChooseCW<C>(#STR, TOWSTRING(#STR))
Run Code Online (Sandbox Code Playgroud)
再次感谢.
使用宏来生成字符串的两种形式,并使用模板函数来选择要使用的形式.
template<typename C>
const C * ChooseCW(const char * c, const wchar_t * w);
template<>
const char * ChooseCW<char>(const char * c, const wchar_t * w)
{
return c;
}
template<>
const wchar_t * ChooseCW<wchar_t>(const char * c, const wchar_t * w)
{
return w;
}
#define CW(C, STR) ChooseCW<C>(STR, L##STR)
insert(value_type(CW(C, "in1"), CW(C, "out1")));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1521 次 |
| 最近记录: |