Fra*_*ank 6 c++ c-preprocessor preprocessor-directive
我坐在一些遗留代码上,通过#defines生成大量代码.现在我知道不可能有一个#ifdef内部#define,但#if可能吗?我想为特定类型添加一些特化.(没有像使用模板那样进行重大更改).以下示例给出了隐秘错误,因此不是这样的:
#define MK_GET(type) \
type get_ ## type (int index) \
{ \
#if type == double \ <-- what i want to add
specialized code... \
#endif
...
} \
MK_GET(double);
MK_GET(int);
MK_GET(string);
Run Code Online (Sandbox Code Playgroud)
您可以使用模板实现:
template<typename T>
struct getter
{
T operator()(int index)
{
// general code
}
};
template<>
struct getter<double>
{
T operator()(int index)
{
// specialized code
}
};
#define CAT(a, b) a ## b
#define MK_GET(type) type CAT(get_, type) (int index) getter<type>()(index)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5797 次 |
| 最近记录: |