Joh*_*erg 3 c++ templates metaprogramming c++11
我试图T从一个类型中推断出底层模板类型E = T<T2,T3>.这将使例如可以制作模板函数pair_maker(const E&a),其可以与几种类似类型的容器中的一种一起使用.粗糙的元代码:
template <typename T>
auto pairmaker(const E & a) -> PairContents<E,std::string>::type {
ContainerPairMaker<E,std::string>::type output;
... some code ...
return output;
}
Run Code Online (Sandbox Code Playgroud)
PairContents<E,std::string>
将类型vector<int>转换为vector<pair(int,std::string)>或whatever<T1>转换为whatever<pair(T1,std::string)>.
类型剖析的另一个类似示例是std :: array(或类似的容器),我想在其中找出容器类型来创建一个新的类似数组.例如,对于这些类型的功能(这是现在的实际工作代码)
template <typename T >
auto make_some3(const T & a)
-> std::array<typename T::value_type,10*std::tuple_size<T>::value>{
return std::array<typename T::value_type,10*std::tuple_size<T>::value>{} ;
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我所追求的是自动显式使用'std :: array'.
对于std :: array,有tuple_size特性有帮助,类似的东西可以用于查找type任何第二个参数,但我再也想不出任何用于查找容器类型的东西.
总结一下:什么样的机器(如果有的话)可以用于这样的情况.在多大程度上可以处理模板参数,模板模板参数,任意数量的参数和未知类型的非模板参数的混合.
Ker*_* SB 13
这是一个想法:
template <typename T, typename ...>
struct tmpl_rebind
{
typedef T type;
};
template <template <typename ...> class Tmpl, typename ...T, typename ...Args>
struct tmpl_rebind<Tmpl<T...>, Args...>
{
typedef Tmpl<Args...> type;
};
Run Code Online (Sandbox Code Playgroud)
用法:
typedef std::vector<int> IV;
typedef typename tmpl_rebind<IV, std::pair<double, std::string>>::type PV;
Run Code Online (Sandbox Code Playgroud)
现在PV = std::vector<std::pair<double, std::string>>.