Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过替换类型重写参数包_C++_Variadic Templates - Fatal编程技术网

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
的模板吗?