是否有这样的解决方案来循环使用模板化 int 参数的函数,该body()函数forIdx不需要在任何时候需要使用新函数创建一个带有函数的新结构?C++20 中的模板化 lambdas 看起来很有前途,但似乎不可能指定不会自动推导出的模板参数。
struct LoopFunc {
template <int i>
void body() {
std::cout << i;
};
};
template<int i>
struct forIdx {
template<typename T>
static void loop(T&& func) {
func.body<i>();
forIdx<i - 1>::loop(func);
}
};
template<>
struct forIdx<-1> {
template<typename T>
static void loop(T&& func) {};
};
int main() {
forIdx<10>::template loop(LoopFunc{});
}
Run Code Online (Sandbox Code Playgroud)
该函数用于创建元组元素的笛卡尔积。DirectProduct包含所有具有静态generateAllElements()功能的元素。
struct CrossProduct {
std::tuple<MockElement...> vals;
std::set<DirectProduct> result;
template <int num>
void body() {
if (result.empty()) …Run Code Online (Sandbox Code Playgroud) c++ templates template-meta-programming variadic-templates c++20