假设我有以下可变参数模板结构:
template <class... T>
struct Example {};
现在我想定义一个模板函数:
template<class... S>
??? f() {
return Example<???>
}
Example<>
的特化所在取决于模板参数 S
的 f
.
为了更具体(和简单),现在我只想返回 Example<int, ...,int>
,其中 int
的数量是参数包的大小S
.
如何在现代 C++(即 C++11/14/17)中完成?
更一般地说,有没有办法在编译时根据模板参数定义函数?
最佳答案
您可以创建映射类型:
// Maps some type T to the type U.
template <typename T, typename U>
using Map = U;
您可以按如下方式使用:
template<class... S>
Example<Map<S, int>...> f() {
return Example<Map<S, int>...>{};
}
关于c++ - 如何在现代 C++ 中基于另一组可变参数模板参数来表示可变参数模板类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43295622/