模板特化的实例化不正确

kal*_*alj 2 c++ template-specialization

在下面的代码中,我尝试A在模板参数T具有值时为基本情况专门化类B<m>.

#include <array>
#include <iostream>

template <std::size_t m>
struct B
{
  std::array<double,m> arr;
};


template <std::size_t n, typename T>
struct A
{
  std::array<T,n> arr;
  const static int inner_dim = T::inner_dim;
};

template <std::size_t n >
template <std::size_t m>
struct A<n,B<m>>
{
  std::array<B<m>,n> arr;
  const static int inner_dim = m;
};


int main(int argc, char *argv[])
{
  A<5,A<4,B<3>>> a;
  std::cout << a.inner_dim << std::endl;

  A<5,B<4>> b;
  std::cout << b.inner_dim << std::endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,在main中的实例化中,当我用g ++ 5.4编译时,我得到以下错误:

$ g++ -Wall --std=c++11 specialization.cc 
specialization.cc: In instantiation of ‘const int A<4ul, B<3ul> >::inner_dim’:
specialization.cc:15:20:   required from ‘const int A<5ul, A<4ul, B<3ul> > >::inner_dim’
specialization.cc:30:18:   required from here
specialization.cc:15:20: error: ‘inner_dim’ is not a member of ‘B<3ul>’
   const static int inner_dim = T::inner_dim;
                    ^
specialization.cc: In instantiation of ‘const int A<5ul, B<4ul> >::inner_dim’:
specialization.cc:33:18:   required from here
specialization.cc:15:20: error: ‘inner_dim’ is not a member of ‘B<4ul>’
Run Code Online (Sandbox Code Playgroud)

似乎只使用了一般定义,而不是专业化.如何确保实例化正确的专业化?

w1c*_*h3r 6

我的猜测是

template <std::size_t n >
template <std::size_t m>
struct A<n,B<m>>
Run Code Online (Sandbox Code Playgroud)

应该

template <std::size_t n, std::size_t m>
struct A<n,B<m>>
Run Code Online (Sandbox Code Playgroud)