Why is it disallowed for partial specialization in a non-type argument to use nested template parameters

前端 未结 2 1052
清歌不尽
清歌不尽 2020-12-03 00:33

I have this code

template
struct A;

template
struct A {
  /* ... */
};

// should work
A<25&g         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 01:29

    Partial specialisation requires that the non-type template argument be resolvable at compile time.

    At this point

    template
    struct A {
      /* ... */
    };
    

    N is a variable that can take more than one value and the compiler is unable to calculate N % 5 with certainty.

    Your example instantiates a using

    A<25> a;
    

    but you could also have

    A<25> a1;
    A<15> a2;
    

    How does the compiler choose a value for N in this scenario? It can't and so it has to disallow the code.

提交回复
热议问题