c++串联和字符串化操作符
C++ concatenation and strinization operator
我想创建一个快捷方式来进行类似的函数调用
int g[2];
void f1() {
g[0] = 1;
}
void f2() {
g[1] = 2;
}
void (*f)();
由于函数f1, f2有一些共同的模式,我可以通过c++连接(##)和字符串化(#)操作符或其他方式创建一个快捷方式,如:
for (int i = 0; i < 2; i++) {
// have to do something to get the function name formed like f1, f2
// and then assign f to f1 or f2 and call f, based on value of i
// need help in this portion
}
template<size_t i>
void f()
{
g[i-1] = i;
}
f<1>();
f<2>();
通过使用函数指针数组
,可以使用以下方法for (int i = 0; i < 2; i++) {
void ( *pf[] )() = { f1, f2 };
pf[i]();
}
相关文章:
- c++中从整数到字符串的转换中,左移操作符是做什么的?
- 使用+操作符进行字符串连接
- 为什么添加两个字符串字面值不使用操作符+
- c++操作符重载和字符串集中
- C搜索字符串如果类型字符,int类型,操作符类型
- 使用操作符[]将字符串放入映射中
- c++在输入和使用操作符中查找字符串
- c++中的C风格字符串和操作符重载
- 如何重载+=操作符以返回字符串
- c++中的宏操作符和std::字符串比较
- 将字符串转换为操作符
- 将facet应用于所有流输出,使用自定义字符串操作符
- 使用jsoncpp库使用/操作符创建JSON字符串时出错
- 字符串操作符
- c++串联和字符串化操作符
- c++字符串和重载操作符
- 创建了自己的字符串类——重载赋值操作符和析构函数的错误
- 二元操作符重载原语的字符串类
- 字符串比较的小于操作符无论在什么情况下都会产生相同的结果
- 字符串的操作符重载