考虑到模板参数,正在更改if语句条件
Altering if-statement condition in consideration of template argument
我有一个带有模板参数的全局内核函数:
template<int ARG> __global__ void kernel(array[]) {
int threadID = blockDim.x*blockIdx.x + threadIdx.x;
if(...) {...}
}
考虑到模板参数,函数和特殊if语句条件的行为略有不同,但主体保持不变。比方说:ARG == 0
if语句看起来像:if(expr1){body}
ARG == 1
if语句看起来像:if(expr2){body}
ARG == 2
if语句看起来像:if(expr1 && expr2){body}
我的问题是(从可读性和性能的角度(提供这一功能的最佳方式是什么?
编辑:表达式expr1
和expr2
是对__device__ boolean
函数的调用,例如fnc1(array[threadID])
和fnc2(array[threadID])
。
直接的方法是暴力:
if ((ARG != 1 || expr1) && (ARG != 0 || expr2)) ...
由于ARG在编译时是已知的,因此编译器将在这里生成良好的代码。
您可以声明一个辅助类模板:
template<int ARG>
class IfCondition {
};
并针对CCD_ 12:的不同值进行专门化
template<>
class IfCondition<0> {
public:
static bool Get() {
return expr1;
}
};
template<>
class IfCondition<1> {
public:
static bool Get() {
return expr2;
}
};
template<>
class IfCondition<2> {
public:
static bool Get() {
return expr1 && expr2;
}
};
然后在你的模板中使用它,如下所示:
if (IfCondition<ARG>::Get())
...
}
它的好处是,有了内联,它将像真正编写if(expr1) {body}
或if (expr2) {body}
之类的东西一样快。
编辑
另一种方法是使用模板函数专业化:
template<int ARG>
bool ifCondition() { return false; }
template<>
bool ifCondition<0>() { return expr1; }
template<>
bool ifCondition<1>() { return expr2; }
template<>
bool ifCondition<2>() { return expr1 && expr2; }
// Then later, inside your template:
if (ifCondition<ARG>()) {
...
}
ARG==0 if语句看起来像:if(expr1({body}
ARG==1if语句看起来像:if(expr2({body}
ARG==2if语句看起来像:if(expr1&&expr2({body}
直接编码,因为这是您自己对可读的解释。
由于ARG
可以在编译时解析,因此它将具有性能。
if ( ARG == 0 && expr1 ) {body}
if ( ARG == 1 && expr2) {body}
if ( ARG == 2 && expr1 && expr2 ) {body}
或者,如果{body}
很重,则将它们合并。
if ( ARG == 0 && expr1 ) ||
( ARG == 1 && expr2) ||
( ARG == 2 && expr1 && expr2 ) {body}
相关文章:
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复