推导出模板参数的模板参数

Mos*_*sid 2 c++ templates

有一个类 template A,它以模板类作为其模板参数。

template <typename T> class A {}
template <typename T> class B {}
int main()
{
    A<B<int>>();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

类模板能否A推导出其模板参数(Bint)的模板参数(int)?

或者有没有其他方法可以解决这个问题?例如,

template <typename T<typename U>> class A {}
Run Code Online (Sandbox Code Playgroud)

Tar*_*ama 5

您可以制作A模板模板:

template <template <typename> class T, typename Inner> 
class A<T<Inner>> {};
Run Code Online (Sandbox Code Playgroud)


Yak*_*ont 5

template<class>struct inner{};
template<template<class...>class Z, class T, class...Ts>
struct inner<Z<T, Ts...>>{
  using type=T;
};
template<class Z>
using inner_t=typename inner<Z>::type;
Run Code Online (Sandbox Code Playgroud)

andinner_t<X>是第一个模板参数(X如果存在),否则替换失败。