5

I'm trying to do something along the lines of this using 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 sizeof instead of sizeof... works in this case. Both seem like they could be easily computed during compile time.

Is the compiler correct that I can't use sizeof... in template arguments for specialization?

Pubby
  • 51,882
  • 13
  • 139
  • 180
  • You mean `sizeof(xs)` works? I would expect a complaint about not-expanded packs. – R. Martinho Fernandes Nov 16 '11 at 05:12
  • @R.MartinhoFernandes Strangely it does work. – Pubby Nov 16 '11 at 05:16
  • Same error, simpler code: http://ideone.com/BBKbw. The second may be an error in the compiler, or just that `sizeof(xs) == sizeof(int)` and hence does not involve the template arguments. - As a practical solution: `value = sizeof...(xs) == n;`? – UncleBens Nov 16 '11 at 07:41

1 Answers1

3

I'm going to assume this is a compiler issue after reading this post.

A partially specialized non-type argument expression shall not involve a template parameter of the partial specialization except when the argument expression is a simple identifier.

Which is being disputed here.

GCC is either incorrectly unpacking the parameter pack, or evaluating sizeof prematurely.

Response to bug report I filed may be helpful.

Community
  • 1
  • 1
Pubby
  • 51,882
  • 13
  • 139
  • 180