具有模板模板参数的模板 class 专业化

Template class specialization with a template template argument

#include <tuple>
#include <iomanip>

template <typename T, typename ...L>
struct foo{};

template <typename T>
struct bar{
    using toto = T;
};

template <template<typename T, typename ...L> class F>
struct bar<F>{
    using toto = T
};

int main(){
    bar<foo<int,char,char>> a;
}

当参数是一个 class 且至少有一个模板参数 <typename T, typename ...L>

时,我想专门化 bar

我试过了:

template <template<typename T, typename ...L> class F>
struct bar<F<T,L...>>{
    using toto = T
};

template <template<typename , typename ...> class F, typename T, typename ...L>
struct bar<F<T,L...>>{
    using toto = T
};

这可能有道理,但我没弄对

您的 ideone 代码只是有一堆印刷错误:

struct bar<F<T,...L>>{
//should be
struct bar<F<T,L...>>{

//missing brackets
int main{

//missing semicolon
using toto = T

bar<foo, int,char,char> a;
//should be
bar<foo<int,char,char>> a;

你在样本中忘记了很多东西,语法上说起来

template <typename T, typename... L>
struct foo{};

template <typename T>
struct bar {
    using toto = T; // Semicolon missing
};

template <template<typename, typename...> class F, typename T, typename... L>
struct bar<F<T,L...>> { // Wrong pack expansion
    using toto = T;
};

int main() { // () missing

    bar< foo<int,char,char> > a; // Pass the parameters to foo since you're
                                 // partially specializing bar to just do that
}

Example on ideone

此处存在一些语法问题。

  1. bar 是一个接受一个类型参数的模板。因此 bar 的任何部分或显式特化也必须采用一个类型参数。

    template <template<typename T, typename ...L> class F>
    struct bar<F> {
    

在这里,TL 名称是无关紧要的,实际上您是在专门研究模板模板。那不匹配。您必须专注于 specific F:

的实例化
    template <template<typename , typename ...> class F,
        typename T, typename... L>
    struct bar<F<T, L...>> {
  1. 你少了一个分号:

    using toto = T;
                 ^^
    
  2. 您的 main 声明缺少括号:

    int main() {
        bar<foo<int,char,char>> a;
    }