Size ... allowed in template arguments for specialization?

I am trying to do something in this direction using the GCC 4.7 snapshot:

template <int n, int... xs> struct foo { static const int value = 0; }; // partial specialization where n is number of ints in xs: template <int... xs> struct foo<sizeof...(xs), xs...> { // error: template argument 'sizeof (xs ...)' // involves template parameter(s) static const int value = 1; }; template <int... xs> struct foo<sizeof(xs), xs...> { // This compiles fine. sizeof(xs) is sizeof int // even though packs aren't expanded static const int value = 2; }; 

The error is strange, because in this case sizeof works instead of sizeof .... Both seem so that they can be easily calculated at compile time.

Is the compiler correct that I cannot use sizeof... in template arguments for specialization?

+4
source share
1 answer

I am going to assume that this is a compiler problem after reading this post .

A partially specialized non-character argument expression must not include a partial specialization template parameter, unless the argument expression is a simple identifier.

What is disputed here .

GCC either incorrectly decompresses the parameter package or prematurely evaluates sizeof .

The answer to the bug report I filed may be helpful.

+3
source

Source: https://habr.com/ru/post/1381422/


All Articles