具有模板模板参数的模板类专门化

时间:2015-06-16 14:35:10

标签: c++ templates template-meta-programming

#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;
}

当参数是一个至少有一个模板参数bar

的类时,我想专门化<typename T, typename ...L>

我试过了:

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
};

这可能是有道理的,但我无法做到正确

3 个答案:

答案 0 :(得分:1)

你的构思代码只有一堆印刷错误:

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;

答案 1 :(得分:1)

你在样本上遗忘了很多东西,语法上说话

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

答案 2 :(得分:0)

这里有一些合成问题。

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

    template <template<typename T, typename ...L> class F>
    struct bar<F> {
    
  2. 此处,TL名称无关紧要,而且您真正专注于模板模板。这并不匹配。您必须专注于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;
      }
      
相关问题