如何检查参数包是否具有执行顺序中的确切类型
How to check if parameter pack has exact types in an exect order
>我有一个函数checkTO
,它将函数包作为参数,我想知道这个包是否包含int
、char
和bool
类型,并且按照这个特定的顺序,但同时它们可以放置在任何地方。允许其他相同类型的参数,我只需要知道此顺序中的那 3 个是否存在。
我有这个例子可以完成这项工作。
#include <iostream>
static bool foundInt = false;
static bool foundChar = false;
static bool foundBool = false;
static bool hasTO = false;
void check() {
}
template <typename T>
void check(T value) {
if (hasTO) {
return;
}
if (foundInt && foundChar && foundBool) {
hasTO = true;
return;
}
if (!foundInt || !foundChar) {
hasTO = false;
return;
}
hasTO = std::is_same<T, bool>::value;
}
template <typename First, typename... Rest>
void check(First firstValue, Rest... rest) {
if (!foundInt) {
if (std::is_same<First, int>::value) {
foundInt = true;
}
check(rest...);
} else if (!foundChar) {
if (std::is_same<First, char>::value) {
foundChar = true;
} else {
// args have to be in a special order
if (!std::is_same<First, int>::value) {
foundInt = false;
}
}
check(rest...);
} else if (!foundBool) {
if (std::is_same<First, bool>::value) {
foundBool = true;
hasTO = true;
} else {
// args have to be in a special order
foundInt = false;
foundChar = false;
}
check(rest...);
}
check(rest...);
}
template <typename... T_values>
bool checkTO(const T_values&... args) {
foundInt = false;
foundChar = false;
foundBool = false;
hasTO = false;
check(args...);
return hasTO;
}
int main()
{
int a = 1;
char b = 'c';
bool c = true;
float d = 1.1;
float d1 = 1.1;
float d2 = 1.2;
std::cout << "TRUE1: " << checkTO() << std::endl;
std::cout << "TRUE1: " << checkTO(a, b, c) << std::endl;
std::cout << "TRUE2: " << checkTO(a, a, b, c) << std::endl;
std::cout << "TRUE3: " << checkTO(a, a, b, c, c) << std::endl;
std::cout << "TRUE4: " << checkTO(d, a, b, c, c) << std::endl;
std::cout << "TRUE5: " << checkTO(a, b, d1, a, b, c, d2) << std::endl;
std::cout << "TRUE6: " << checkTO(d1, d2, a, a, a, b, c) << std::endl;
std::cout << "TRUE7: " << checkTO(a, b, c, d1, d2, a, a, b, a, c) << std::endl;
std::cout << "FALSE1: " << checkTO(c, a, b) << std::endl;
std::cout << "FALSE2: " << checkTO(b, c, a) << std::endl;
std::cout << "FALSE3: " << checkTO(d1, a, b) << std::endl;
std::cout << "FALSE4: " << checkTO(a, b, d1, c) << std::endl;
}
输出:
TRUE1: 0
TRUE1: 1
TRUE2: 1
TRUE3: 1
TRUE4: 1
TRUE5: 1
TRUE6: 1
TRUE7: 1
FALSE1: 0
FALSE2: 0
FALSE3: 0
FALSE4: 0
我真的很讨厌这个解决方案,因为它不可扩展(如果我需要检查 44 个参数怎么办?(和全局变量。有没有更聪明的方法?
template<typename...T> struct check;
template<typename A, typename...B> struct check<A,B...> {
static constexpr bool pass = check<B...>::pass;
};
template<typename...R> struct check<int,char,bool,R...> {
static constexpr bool pass = true;
};
template<> struct check<> {
static constexpr bool pass = false;
};
template<typename... T>
constexpr bool check_this(T...) { return check<T...>::pass; }
这使用具有某些模板专用化的template struct
:
- 一个"迭代"参数包
- 一个在找到请求的序列时匹配的序列
- 一个与"基本情况"匹配(例如,未找到匹配项(
使用您的 main 的 ideone 上的活生生的例子(并将我的check_this
重命名为checkTO
(。通过除第一个测试之外的所有测试...为什么checkTO()
返回 true??
相关文章:
- QML按钮点击功能执行顺序
- C++ - scanf() 和 printf() 执行顺序不对
- 递归函数的执行顺序
- 运算符 new 的执行顺序和构造函数的参数
- 如何检查参数包是否具有执行顺序中的确切类型
- std::bind() 参数列表中函子的执行顺序(可能与函数参数的求值顺序无关)
- 了解递归函数的执行顺序
- OpenMP 4.5 任务依赖关系和执行顺序
- 通过调试来检查C 中单行表达式执行顺序的方法
- 遵循 C++ 中的构造函数执行顺序
- 联接线程如何影响主线程中的执行顺序?
- C COUT行为 /执行顺序
- 是定义的函数参数的内部执行顺序
- 强制执行执行顺序
- OpenGL计算着色器中线程的执行顺序
- 按执行顺序创建Pthread
- 逻辑操作员执行顺序
- 如何在使用 std::make_tuple 时避免构造函数的未定义执行顺序
- 提升::thread_specific_ptr/清理与退出执行顺序
- C++ 多线程:执行顺序