我还需要什么来使用可变参数模板继承来创建 lambda 重载?
What else do I need to use variadic template inheritance to create lambda overloads?
我了解使用可变模板参数的递归特性和特定模板实例化的基本概念 "eat" 我对参数列表的逐一排序。
我知道可以编写 lambda 以采用某些类型,然后 return 某些类型。请记住,我仍在学习 C++14 和 C++11,所以我还没有掌握其中一个。
这是我在查看 other Stack Overflow questions 后的尝试:
// For std::string
#include <string>
// For std::cout
#include <iostream>
//Create a generalized list instantiation
template <typename ... F>
struct overload : public F... {
overload(F... f) : F(f)... {}
};
//Create an specific end-case, where we directly
//inherit the () operator in order to inherit
//multiple () overloads
template <typename F>
struct overload : F {
using F::operator();
};
//template function to create an overload
template <class... F>
auto make_overload(F... f) {
return (f...);
}
int main() {
auto f = [](int x,int y) -> int {
return x+y;
};
auto g = [](double x,double y) -> int {
return std::ftoi(x+y);
};
auto h = [](std::string x,std::string y) -> int {
return std::stoi(x+y);
};
//Ah, but this is a function.
auto fgh = make_overload(f,g,h);
std::cout << (fgh(1,2)) << std::endl;
std::cout << (fgh(1.5,2.5)) << std::endl;
std::cout << (fgh("bob","larry")) << std::endl;
}
科里鲁:http://coliru.stacked-crooked.com/a/5df2919ccf9e99a6
我在概念上缺少什么?其他答案可能从表面上简洁地回答了这个问题,但我正在寻找答案为什么我没有想到的解释。如果我明白我需要做 using F::operator()
来继承运算符,并且我正确地指出 return 和参数类型不同,那么我还需要做些什么才能使这项工作正常进行?
这是我的思路:
- 创建一个通用可变参数模板库 class。
- 创建特定的模板案例以重载特定的 lambda
operator()
。
- 创建一个辅助函数以获取可变参数模板参数列表,然后使用它来构造 "overload" class.
- 确保类型是明确的。
你实际上并没有递归。
// primary template; not defined.
template <class... F> struct overload;
// recursive case; inherit from the first and overload<rest...>
template<class F1, class... F>
struct overload<F1, F...> : F1, overload<F...> {
overload(F1 f1, F... f) : F1(f1), overload<F...>(f...) {}
// bring all operator()s from the bases into the derived class
using F1::operator();
using overload<F...>::operator();
};
// Base case of recursion
template <class F>
struct overload<F> : F {
overload(F f) : F(f) {}
using F::operator();
};
我了解使用可变模板参数的递归特性和特定模板实例化的基本概念 "eat" 我对参数列表的逐一排序。
我知道可以编写 lambda 以采用某些类型,然后 return 某些类型。请记住,我仍在学习 C++14 和 C++11,所以我还没有掌握其中一个。
这是我在查看 other Stack Overflow questions 后的尝试:
// For std::string
#include <string>
// For std::cout
#include <iostream>
//Create a generalized list instantiation
template <typename ... F>
struct overload : public F... {
overload(F... f) : F(f)... {}
};
//Create an specific end-case, where we directly
//inherit the () operator in order to inherit
//multiple () overloads
template <typename F>
struct overload : F {
using F::operator();
};
//template function to create an overload
template <class... F>
auto make_overload(F... f) {
return (f...);
}
int main() {
auto f = [](int x,int y) -> int {
return x+y;
};
auto g = [](double x,double y) -> int {
return std::ftoi(x+y);
};
auto h = [](std::string x,std::string y) -> int {
return std::stoi(x+y);
};
//Ah, but this is a function.
auto fgh = make_overload(f,g,h);
std::cout << (fgh(1,2)) << std::endl;
std::cout << (fgh(1.5,2.5)) << std::endl;
std::cout << (fgh("bob","larry")) << std::endl;
}
科里鲁:http://coliru.stacked-crooked.com/a/5df2919ccf9e99a6
我在概念上缺少什么?其他答案可能从表面上简洁地回答了这个问题,但我正在寻找答案为什么我没有想到的解释。如果我明白我需要做 using F::operator()
来继承运算符,并且我正确地指出 return 和参数类型不同,那么我还需要做些什么才能使这项工作正常进行?
这是我的思路:
- 创建一个通用可变参数模板库 class。
- 创建特定的模板案例以重载特定的 lambda
operator()
。 - 创建一个辅助函数以获取可变参数模板参数列表,然后使用它来构造 "overload" class.
- 确保类型是明确的。
你实际上并没有递归。
// primary template; not defined.
template <class... F> struct overload;
// recursive case; inherit from the first and overload<rest...>
template<class F1, class... F>
struct overload<F1, F...> : F1, overload<F...> {
overload(F1 f1, F... f) : F1(f1), overload<F...>(f...) {}
// bring all operator()s from the bases into the derived class
using F1::operator();
using overload<F...>::operator();
};
// Base case of recursion
template <class F>
struct overload<F> : F {
overload(F f) : F(f) {}
using F::operator();
};