在可变参数中强制转换特定类型 [英] Cast specific types in variadic argument
本文介绍了在可变参数中强制转换特定类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个接受可变参数的模板函数。
I have a template function that accepts variadic arguments.
template<typename... Params>
void foo(Params... p);
我想查找所有给定类型的事件( const char *
)中的 Params
替换为其他类型,这些值可以转换为(我自己的 Path
具有构造函数 Path(const char *)
的类。想法是使用
I want to find all occurences of a given type (const char*
) in Params
to replace them with another type, that these values can be cast to (my own Path
class with constructor Path(const char*)
). The idea is to have something like
template<typename... Params>
void foo(Params... p) {
bar<convertCharPointerToPath<Params>...>(p...);
}
如何完成转换?
推荐答案
如果只想转换类型,那只是一个元函数:
If you want to convert types only, it's just a meta-function away:
template<typename T> struct convert {
using type = T;
};
template<> struct convert<char const*> {
using type = Path;
};
template<typename T>
using convertCharPointerToPath = typename convert<T>::type;
现在像在原始帖子中一样在参数包扩展中使用它。
Now use it in your parameter pack expansion, as you do in your original post.
这篇关于在可变参数中强制转换特定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文