C++ 通过替换类型重写参数包
在这里,我想重写一个可变模板参数包,并用另一个类型替换某些类型的引用。下面是一个类似伪代码的示例:C++ 通过替换类型重写参数包,c++,variadic-templates,C++,Variadic Templates,在这里,我想重写一个可变模板参数包,并用另一个类型替换某些类型的引用。下面是一个类似伪代码的示例: #include <string> template <typename Params, typename Args> struct TermRewrite { template <typename... Body> static auto constexpr eval(){ // Fill here... } };
#include <string>
template <typename Params, typename Args>
struct TermRewrite {
template <typename... Body>
static auto constexpr eval(){
// Fill here...
}
};
int main() {
TermRewrite<int, char>::eval<int, std::string, double>();
// should give me pack of types as <char, std::string, double>
// ie. it should replace type int with char in Body...
}
#包括
样板
结构术语重写{
样板
静态自动constexpr eval(){
//在这里填写。。。
}
};
int main(){
TermRewrite::eval();
//应该给我一包的类型
//它应该用正文中的字符替换int类型。。。
}
这样我就可以在最后把这些术语重写链接起来。基本上,我想在转发之前转换可变模板。我怎样才能做到这一点?我想不出一个解决办法。你想知道的是,这是我自己编的练习。另外,您是否对可用性有任何建议,以便更容易链接
termrewite
调用?您使用参数包扩展,并使用带有单个参数的元函数
template<typename From, typename To>
struct replace
{
template<typename T>
using replace_fn = std::conditional_t<std::is_same_v<From, T>, To, T>;
template<typename... Args>
using apply = std::tuple<replace_fn<Args>...>;
};
并用作
replace<char, float>::apply<replace<int, char>::apply<int, char, bool>>
replace::apply
但请注意,这意味着您不是将单个
std::tuple
视为类型,而是将其视为一个类型列表。使用参数包扩展时,元函数只接受一个参数
template<typename From, typename To>
struct replace
{
template<typename T>
using replace_fn = std::conditional_t<std::is_same_v<From, T>, To, T>;
template<typename... Args>
using apply = std::tuple<replace_fn<Args>...>;
};
并用作
replace<char, float>::apply<replace<int, char>::apply<int, char, bool>>
replace::apply
但请注意,这意味着您不是将单个
std::tuple
视为类型,而是将其视为类型列表。您可以专门使用来链接replace::type
的模板吗?您可以专门使用来链接replace::type
的模板吗?